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.

Visualization

Tap to expand
Binary Tree Construction Visualization4(2(3)(1))(6(5))Step 1: Parse Root4Step 2-3: Build Left Subtree4231Step 4-5: Complete with Right Subtree426315🎯 **Key Insight:** Use recursive parsing with index tracking for O(n) efficiency
Understanding the Visualization
1
Start with Root
Parse the first number (4) as the root of our tree
2
Enter Left Subtree
First '(' indicates left child - recursively build subtree 2(3)(1)
3
Build Nested Structure
Continue recursively: node 2 gets left child 3 and right child 1
4
Process Right Subtree
After closing left subtree, build right subtree 6(5)
5
Complete Tree
Final tree has all nodes connected according to parentheses structure
Key Takeaway
🎯 Key Insight: The parentheses structure directly maps to tree relationships - each opening parenthesis starts a subtree, and recursive parsing with a global index eliminates the need for expensive string operations

Time & Space Complexity

Time Complexity
⏱️
O(n)

Each character in the string is processed exactly once

n
2n
Linear Growth
Space Complexity
O(n)

Recursion depth equals tree height, which can be O(n) in worst case

n
2n
Linearithmic Space

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
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