Construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way.
The null node needs to be represented by empty parenthesis pair “()”. Omit all the empty parenthesis pairs that don’t affect the one-to-one mapping relationship between the string and the original binary tree.
Input : Preorder: [1, 2, 3, 4] 1 / \ 2 3 / 4 Output: "1(2(4))(3)" Explanation: Originally it needs to be "1(2(4) ())(3()())", but we need to omit all the unnecessary empty parenthesis pairs. And it will be "1(2(4))(3)". Input : Preorder: [1, 2, 3, null, 4] 1 / \ 2 3 \ 4 Output: "1(2()(4))(3)"
This is opposite of Construct Binary Tree from String with bracket representation
The idea is to do the preorder traversal of the given Binary Tree along with this, we need to make use of braces at appropriate positions. But, we also need to make sure that we omit the unnecessary braces. We print the current node and call the same given function for the left and the right children of the node in that order(if they exist). For every node encountered, the following cases are possible.
Case 1: Both the left child and the right child exist for the current node. In this case, we need to put the braces () around both the left child’s preorder traversal output and the right child’s preorder traversal output.
Case 2: None of the left or the right child exist for the current node. In this case, as shown in the figure below, considering empty braces for the null left and right children is redundant. Hence, we need not put braces for any of them.
Case 3: Only the left child exists for the current node. As the figure below shows, putting empty braces for the right child in this case is unnecessary while considering the preorder traversal. This is because the right child will always come after the left child in the preorder traversal. Thus, omitting the empty braces for the right child also leads to same mapping between the string and the binary tree.
Case 4: Only the right child exists for the current node. In this case, we need to consider the empty braces for the left child. This is because, during the preorder traversal, the left child needs to be considered first. Thus, to indicate that the child following the current node is a right child we need to put a pair of empty braces for the left child.
Time complexity : O(n) The preorder traversal is done over the n nodes.
Space complexity : O(n). The depth of the recursion tree can go upto n in case of a skewed tree.
- Find an equal point in a string of brackets
- Balance a string after removing extra brackets
- Construct Binary Tree from String with bracket representation
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Minimum swap required to convert binary tree to binary search tree
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Binary Tree to Binary Search Tree Conversion
- Check whether a given binary tree is skewed binary tree or not?
- Check if a binary tree is subtree of another binary tree | Set 2
- Check whether a binary tree is a full binary tree or not
- Binary Tree to Binary Search Tree Conversion using STL set
- Check if a binary tree is subtree of another binary tree | Set 1
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.