Construct Binary Tree from String - Problem
Construct Binary Tree from String

Imagine you have a serialized representation of a binary tree encoded as a string using parentheses notation - similar to how mathematical expressions are written! Your task is to deserialize this string back into the original binary tree structure.

The string format follows these rules:
• Each node value is an integer
• Left and right children are enclosed in parentheses ()
• If a node has children, the left child always comes first
• Empty parentheses () represent null nodes

Examples:
"4(2(3)(1))(6(5))" represents a tree with root 4, left subtree 2 with children 3 and 1, and right subtree 6 with left child 5
"4(2()(1))" has root 4, left child 2 where 2 has no left child but has right child 1

Your goal is to parse this string and construct the corresponding binary tree, returning the root node.

Input & Output

example_1.py — Basic Tree Construction
$ Input: s = "4(2(3)(1))(6(5))"
Output: Tree with root 4, left subtree 2 with children 3 and 1, right subtree 6 with left child 5
💡 Note: The string represents a complete binary tree. Root is 4, left child is 2 (which has left child 3 and right child 1), right child is 6 (which has left child 5 and no right child).
example_2.py — Tree with Missing Left Child
$ Input: s = "4(2()(1))(6(5))"
Output: Tree where node 2 has no left child but has right child 1
💡 Note: The empty parentheses () indicate a missing left child. Node 2 has null as left child and 1 as right child, while maintaining the same structure elsewhere.
example_3.py — Single Node Tree
$ Input: s = "-4"
Output: Tree with single node containing value -4
💡 Note: A simple case with just one node containing a negative number. No parentheses means no children, so it's just a root node with value -4.

Constraints

  • 0 ≤ s.length ≤ 3 × 104
  • s consists of digits, '(', ')', and '-' only
  • Node values can be negative integers
  • Input string represents a valid binary tree structure

Visualization

Tap to expand
Construct Binary Tree from String INPUT s = "4(2(3)(1))(6(5))" String Structure: 4 (2(3)(1)) (6(5)) Notation Rules: • Integer = node value • (...) = child subtree • 1st (...) = left child • 2nd (...) = right child Input String: "4(2(3)(1))(6(5))" ALGORITHM STEPS 1 Parse Root Value Read digits until '(' found 4 2 Find Left Subtree Match parentheses for left (2(3)(1)) 3 Find Right Subtree Remaining string for right (6(5)) 4 Recurse Build left/right recursively Recursive Tree Building 4 2 6 FINAL RESULT Constructed Binary Tree 4 2 6 3 1 5 Tree Structure: Root: 4 Left: 2 (children 3,1) Right: 6 (left child 5) OK - Tree Built Successfully! Key Insight: Use a stack or recursion to track parentheses depth. When you encounter '(', dive deeper into a subtree. Match opening/closing parentheses to find subtree boundaries. First child in parentheses is always left, second (if exists) is right. Time: O(n), Space: O(n) for recursion stack. TutorialsPoint - Construct Binary Tree from String | Optimal Recursive Solution
Asked in
Google 35 Facebook 28 Amazon 22 Microsoft 18
42.0K Views
Medium Frequency
~15 min Avg. Time
1.9K 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