Construct String from Binary Tree - Problem

You need to construct a string representation of a binary tree following specific formatting rules based on preorder traversal.

The Rules:

  • Each node is represented by its integer value
  • If a node has children, wrap them in parentheses ()
  • Key rule: Omit empty parentheses () EXCEPT when a node has a right child but no left child - then you MUST include () to show the missing left child

Examples:

  • Node with left child only: 1(2)
  • Node with right child only: 1()(3) โ† Notice the empty ()
  • Node with both children: 1(2)(3)

This ensures a unique string representation that can be converted back to the original tree structure.

Input & Output

example_1.py โ€” Basic Tree with Both Children
$ Input: root = [1,2,3,null,4]
โ€บ Output: "1(2()(4))(3)"
๐Ÿ’ก Note: Node 1 has both children 2 and 3. Node 2 has only right child 4, so we need empty () for missing left child, resulting in ()(4). Node 3 has no children.
example_2.py โ€” Simple Tree
$ Input: root = [1,2,3,4]
โ€บ Output: "1(2(4))(3)"
๐Ÿ’ก Note: Node 1 has children 2 and 3. Node 2 has only left child 4, so no empty parentheses needed. Node 3 has no children.
example_3.py โ€” Single Node
$ Input: root = [1]
โ€บ Output: "1"
๐Ÿ’ก Note: Single node with no children requires no parentheses at all.

Visualization

Tap to expand
Binary Tree to String Conversion1234nullConversion Process:1. Start with root: "1"2. Has children, add parentheses3. Left child: "1(2"4. Node 2 has no left, but has right: "1(2()(4)"5. Close left subtree: "1(2()(4))"6. Add right child: "1(2()(4))(3)"Final: "1(2()(4))(3)"๐Ÿ”‘ Key InsightEmpty parentheses () are ONLY needed when:โ€ข A node has a RIGHT child but NO LEFT childโ€ข This preserves the tree structure for reconstruction
Understanding the Visualization
1
Start at Root
Begin with root node value and check for children
2
Process Left Subtree
Recursively convert left subtree to string
3
Handle Missing Left Child
Add empty () when right child exists but left doesn't
4
Process Right Subtree
Convert right subtree if it exists
5
Combine Results
Assemble final string with proper parentheses
Key Takeaway
๐ŸŽฏ Key Insight: The empty parentheses () act as placeholders to maintain the left-right child order, ensuring the string can be uniquely converted back to the original tree structure.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Visit each node exactly once in the tree

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Recursion stack depth equals tree height, plus string storage

n
2n
โšก Linearithmic Space

Constraints

  • The number of nodes in the tree is in the range [1, 104]
  • -1000 โ‰ค Node.val โ‰ค 1000
  • Tree is guaranteed to be a valid binary tree
Asked in
Amazon 45 Facebook 38 Google 32 Microsoft 28
67.9K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen