Open In App

Construct Binary Tree from String with bracket representation

Last Updated : 21 May, 2025
Comments
Improve
Suggest changes
2 Likes
Like
Report

Given a string consisting of parenthesis and integers, representing a binary tree. The task is to construct a binary tree from this string.

The string contains an integer followed by zero, one or two pairs of parenthesis. The integer represents the root's value and a pair of parenthesis contains a child binary tree with the same structure. Always start to construct the left child node of the parent first if it exists.

Examples: 

Input : "1(2)(3)"
Output : 1 2 3
Explanation :

122222

Input : "4(2(3)(1))(6(5))"
Output : 4 2 3 1 6 5
Explanation :

1

Idea:

In a binary tree string representation, each part begins with a number representing the node's value, followed by at most two pairs of parentheses. The first pair of parentheses after a number contains the complete left subtree of that node (if it exists), and the second pair of parentheses contains the complete right subtree (if it exists). Each subtree follows the same pattern recursively, making it a self-similar structure.

[Naive Approach] Using Recursion - O(n^2) time and O(n) space:

The idea is to recursively parse the string by first extracting the root value (which could be multiple digits) and then finding matching pairs of parentheses that enclose the left and right subtrees.

For each subtree enclosed in parentheses, we find its corresponding closing parenthesis using a counter (incrementing for '(' and decrementing for ')'), which helps us identify the correct substring for the left child.

Once we have the left subtree's closing index, we can process the right subtree starting two positions after (to skip the closing parenthesis and opening parenthesis of right subtree).

This process continues recursively until we either encounter an empty substring (base case) or process all characters, effectively building the tree from root to leaves.

C++
Java Python C# JavaScript

Output
4 2 6 3 1 5 N N N N N N N 

[Expected Approach] Using Pre-Order Traversal - O(n) time and O(n) space

The idea is to traverse the string sequentially using a single index passed by reference, constructing the tree in a preorder manner (root-left-right) where we first extract the root value by parsing consecutive digits, then recursively build the left subtree if we encounter an opening parenthesis, and finally build the right subtree if another opening parenthesis exists after completing the left subtree construction.

Step by step approach:

  1. Maintain a single index that traverses through the string from left to right.
  2. When encountering digits, parse them to form the complete node value and create a new node with the parsed value.
  3. If an opening parenthesis '(' is found, increment index and recursively build left subtree. After left subtree is built and index is incremented past closing parenthesis, check for another opening parenthesis.
  4. If second opening parenthesis exists, increment index and recursively build right subtree.
  5. Return the constructed node which serves as root for its subtree
C++
Java Python C# JavaScript

Output
4 2 6 3 1 5 N N N N N N N 

 


Next Article
Article Tags :
Practice Tags :

Similar Reads