Construct String from Binary Tree - Problem

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 following the left child.

Omitting Empty Parentheses: Any empty parentheses pairs () should be omitted from the final string representation, with one specific 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.

Input & Output

Example 1 — Tree with Mixed Children
$ Input: root = [1,2,3,null,4]
Output: "1(2()(4))(3)"
💡 Note: Node 1 has both children. Node 2 has only right child (4), so we include empty parentheses for the missing left child to maintain structure. Node 3 has no children.
Example 2 — Tree with Only Left Children
$ Input: root = [1,2,3,4]
Output: "1(2(4))(3)"
💡 Note: Node 1 has both children. Node 2 has only left child (4), so no empty parentheses needed. Node 3 has no children.
Example 3 — Single Node
$ Input: root = [1]
Output: "1"
💡 Note: Single node with no children requires no parentheses.

Constraints

  • The number of nodes in the tree is in the range [1, 104]
  • -1000 ≤ Node.val ≤ 1000

Visualization

Tap to expand
Construct String from Binary Tree INPUT Binary Tree Structure: 1 2 3 4 null root = [1,2,3,null,4] Preorder: root --> left --> right ALGORITHM STEPS (Optimized DFS) 1 Visit Node Add node value to result 2 Process Left Child Add () if exists, recurse 3 Handle Empty Left Add () if right exists 4 Process Right Child Add () if exists, recurse Traversal Order: 1. Visit 1 --> "1" 2. Visit 2 --> "1(2" 3. No left --> "1(2()" 4. Visit 4 --> "1(2()(4))" 5. Visit 3 --> "1(2()(4))(3)" FINAL RESULT String Construction: "1" "(2()(4))" empty left: () right child: (4) "(3)" Combined Output: "1(2()(4))(3)" OK - Verified Time: O(n) | Space: O(h) Key Insight: Empty parentheses "()" are only needed when a node has NO left child but HAS a right child. This preserves tree structure: "()" acts as placeholder for missing left subtree. DFS preorder ensures correct order. TutorialsPoint - Construct String from Binary Tree | Optimized DFS Approach
Asked in
Amazon 35 Facebook 28 Google 22
67.0K Views
Medium Frequency
~15 min Avg. Time
1.5K 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