Construct String from Binary Tree

Given the `root` node of a binary tree, your task is to create a string representation of the tree following a specific set of formatting rules. The representation should be based on a preorder traversal of the binary tree and must adhere to the following guidelines: - **Node Representation:** Each node in the tree should be represented by its integer value. - **Parentheses for Children:** If a node has at least one child (either left or right), its children should be represented inside parentheses. Specifically: if a node has a left child, the value of the left child should be enclosed in parentheses immediately following the node's value; if a node has a right child, the value of the right child should also be enclosed in parentheses, and the parentheses for the right child should follow those of the left child. - **Omitting Empty Parentheses:** Any empty parentheses pairs `()` should be omitted from the final string representation of the tree, with one exception: when a node has a right child but no left child, you must include an empty pair of parentheses to indicate the absence of the left child. This ensures that the one-to-one mapping between the string representation and the original binary tree structure is maintained. In summary, empty parentheses pairs should be omitted when a node has only a left child or no children. However, when a node has a right child but no left child, an empty pair of parentheses must precede the representation of the right child to reflect the tree's structure accurately.

30:00

Construct String from Binary Tree
medium
Free written solution
Topics
Companies

Given the root node of a binary tree, your task is to create a string representation of the tree following a specific set of formatting rules. The representation should be based on a preorder traversal of the binary tree and must adhere to the following guidelines:

  • Node Representation: Each node in the tree should be represented by its integer value.
  • Parentheses for Children: If a node has at least one child (either left or right), its children should be represented inside parentheses. Specifically: if a node has a left child, the value of the left child should be enclosed in parentheses immediately following the node's value; if a node has a right child, the value of the right child should also be enclosed in parentheses, and the parentheses for the right child should follow those of the left child.
  • Omitting Empty Parentheses: Any empty parentheses pairs () should be omitted from the final string representation of the tree, with one exception: when a node has a right child but no left child, you must include an empty pair of parentheses to indicate the absence of the left child. This ensures that the one-to-one mapping between the string representation and the original binary tree structure is maintained.

In summary, empty parentheses pairs should be omitted when a node has only a left child or no children. However, when a node has a right child but no left child, an empty pair of parentheses must precede the representation of the right child to reflect the tree's structure accurately.

Example 1:
Input: {"root":[1,2,3,4]}
Output: "1(2(4))(3)"
Constraints:
  • The number of nodes in the tree is in the range [1,104][1, 10^4].

  • −1000≤Node.val≤1000-1000 \leq \text{Node.val} \leq 1000

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"root":[1,2,3,4]}

Start with an empty output. A preorder walk writes each value, then its left child in parentheses, then its right child in parentheses.

1234

output string: bar height = nesting depth = tree depth

(empty)

open calls

none

nodes written: 0 / 4

current node and its tokens
open walk() calls
required empty "()"
Variables
VariableValue
nodenull
out
depth0
DepthFunction Call
Stack empty
0/14