


You are given the ‘POSTORDER’ and ‘PREORDER’ traversals of a binary tree. The binary tree consists of ‘N’ nodes where each node represents a distinct positive integer named from ‘1’ to ‘N’. The task is to return the root node of any binary tree that matches the given ‘POSTORDER’ and ‘PREORDER’ traversals.
Example:‘POSTORDER’ = [4, 5, 2, 6, 7, 3, 1]
‘PREORDER’ = [1, 2, 4, 5, 3, 6, 7]
A binary tree that matches the given ‘POSTORDER’ and ‘PREORDER’ traversal is:

So, create this binary tree and return the root node ‘1’.
Note:
1. You can return any binary tree that matches the given ‘POSTORDER’ and ‘PREORDER’ traversals.
2. You can always construct a valid binary tree from the ‘POSTORDER’ and ‘PREORDER’ traversals.
The first line of input contains an integer ‘T’ which denotes the number of test cases. Then, the ‘T’ test cases follow.
The first line of each test case contains an integer ‘N’ denoting the number of nodes in the binary tree.
The second line of each test case contains ‘N’ space-separated integers denoting the ‘POSTORDER’ traversal of the binary tree.
The third line of each test case contains ‘N’ space-separated integers denoting the ‘PREORDER’ traversal of the binary tree.
Output format:
For every test case, return the root node of the newly created binary tree. The printed output will be the ‘POSTORDER’ and ‘PREORDER’ traversals of the returned tree, each printed on a new line.
You do not need to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 10^3
Time limit: 1 second
2
6
6 2 4 5 3 1
1 4 2 6 3 5
5
1 5 3 2 4
4 1 2 3 5
6 2 4 5 3 1
1 4 2 6 3 5
1 5 3 2 4
4 1 2 3 5
Test Case 1:
‘POSTORDER’ = [6, 2, 4, 5, 3, 1]
‘PREORDER’ = [1, 4, 2, 6, 3, 5]
A binary tree that matches the given ‘POSTORDER’ and ‘PREORDER’ traversal is:

So, create this binary tree and return the root node ‘1’.
Test Case 2:
‘POSTORDER’ = [1, 5, 3, 2, 4]
‘PREORDER’ = [4, 1, 2, 3, 5]
A binary tree that matches the given ‘POSTORDER’ and ‘PREORDER’ traversal is:

So, create this binary tree and return the root node ‘4’.
2
4
3 4 1 2
2 1 3 4
5
5 4 3 2 1
1 2 3 4 5
3 4 1 2
2 1 3 4
5 4 3 2 1
1 2 3 4 5
For any node in ‘PREORDER’ traversal, try to find the number for nodes in its left subtree from the ‘POSTORDER’ traversal.
We can create a unique binary tree from (‘INORDER’ and ‘PREORDER’) traversals or
(‘INORDER’ and ‘POSTORDER’) traversals. But, (‘POSTORDER’ and ‘PREORDER’) traversals don’t give us enough information to create a unique binary tree. For example, the following two trees produce the same ‘POSTORDER’ and ‘PREORDER’ traversal sequence:
‘PREORDER’ = [1, 2, 3, 4]
‘POSTORDER’ = [4, 3, 2, 1]
So, we create the binary tree assuming that ‘PREORDER[i+1]’ is always the left child of ‘PREORDER[i]’.
[ ROOT NODE ] [ LEFT SUBTREE ...] [ RIGHT SUBTREE ...] => PREORDER
[ LEFT SUBTREE ...] [ RIGHT SUBTREE ...] [ ROOT NODE ] => POSTORDER
We know that the root of the tree is the first element in ‘PREORDER’ traversal, i.e., ‘PREORDER[0]’ and the last element in ‘POSTORDER’ traversal,i.e., ‘POSTORDER[n-1]’. Then, the next element in ‘preorder’, i.e., ‘preorder[1]’, is the left child of the root node. So, all nodes before index ‘i’ in ‘POSTORDER’ (where ‘POSTORDER[i]’ = ‘preorder[1]’) must be present in the left subtree of the root node. All nodes from index ‘i+1’ to index ‘n-3’ must be present in the right subtree (because ‘POSTORDER[n-2]’ is the right child of the root node). Now, we recursively build the left and right subtree and link them to the root node. Consider the following example:
‘PREORDER’ = [1, 2, 4, 5, 3, 6, 7, 8]
‘POSTORDER’ = [4, 5, 2, 7, 8, 6, 3, 1]
From ‘PREORDER’, we get that ‘2’ is the left child of the root node. The left subtree size is three as there are two elements before ‘2’ in ‘POSTORDER’.
Left child of ROOT: ‘2’, and left subtree:
‘PREORDER’ = [2, 4, 5]
‘POSTORDER’ = [4, 5, 2]
In ‘POSTORDER’, the nodes after ‘2’ up to the second last element belong to the right subtree. The second last element in ‘POSTORDER’ is the right child of the root node. Therefore, there are four nodes in the right subtree.
Right child of ROOT: ‘3’, and right subtree:
‘PREORDER’ = [3, 6, 7, 8]
‘POSTORDER’ = [7, 8, 6, 3]
Link the root node to its left and right child. Similarly, solve for the left subtree and the right subtree recursively. Write a recursive helper function ‘BUILD_TREE’ which takes three parameters starting index of ‘POSTORDER’ (i.e., ‘POST_INDEX’), starting index of ‘PREORDER’ (i.e., ‘ST’), and the last index of ‘PREORDER’ (i.e., ‘END’).
The ‘BUILD_TREE(integer POST_INDEX, integer ST, integer END)’ function:
O(N^2), where ‘N’ is the number of nodes in the binary tree.
In the worst-case scenario, the generated tree will be left-skewed. So, the left child of the root node ‘POSTORDER[N-1]’ is stored at ‘POSTORDER[N-2]’ (we find it in ‘N-2’ iterations), the left child of ‘POSTORDER[N-2]’ is store in ‘POSTORDER[n-3]’ (we find it in ‘N-3’ iterations) and so on. So, the total number of iterations is approx. ‘(N*(N+1))/2’,i.e., ‘O(N^2)’.
O(HEIGHT), where ‘HEIGHT’ is the height of the binary tree.
The size of the recursion stack will be equal to the height of the binary tree created.