You are given arrays 'inOrder' and 'postOrder', which represent 'inorder' traversal and 'postorder' traversal of a 'Binary Tree' respectively.
Construct a 'Binary Tree' represented by the given arrays and return it's head.
Assume that the Binary Tree contains only unique elements.
Input: 'inOrder' = [9, 3, 15, 20, 7], 'postOrder' = [9, 15, 7, 20, 3]
Output:
We get the following binary tree from Inorder and Postorder traversal:
The first line of each test case contains an integer 'n' which represents the number of nodes in the Binary Tree.
The next line of each test case contains 'n' single space-separated integers, representing the Postorder traversal of the Binary Tree.
The next line of each test case contains 'n' single space-separated integers, representing the Inorder traversal of the Binary Tree.
Return the head of the binary tree constructed.
The level order traversal of the Binary Tree is printed.
You do not need to print anything; it has already been taken care of. Just implement the given function.
7
4 5 2 6 7 3 1
4 2 5 1 6 3 7
1 2 3 4 5 6 7
We get the following Binary Tree from the given Inorder and Postorder traversal:
6
2 9 3 6 10 5
2 6 3 9 5 10
5 6 10 2 3 9
We get the following Binary Tree from the given Inorder and Postorder traversal:
Try to solve this in O(n).
1 <= 'n' <= 10000
1 <= 'inOrder[i]' , ‘postOrder[i]’ <= 100000
Time Limit: 1 second
Think of the Depth First Search approach.
To solve this problem, first, we take the last element in the ‘POST_ORDER’ array/list as the root because we know post-order traversal is like the first traverse of the left subtree nodes and then the right subtree node at the end traverse root. Hence the root is present as the last element of post-order traversal. Then we find the position of the root data in the ‘IN_ORDER’ array/list. Let’s assume at the ‘Kth’ index we find the root data in the ‘IN_ORDER’ array/list. Then we locate the range for the left subtree and right subtree and recursively call the function for finding the left subtree and the right subtree.
For example:
First, we select the last element of the ‘POST_ORDER’ array/list as a root. Then we find the 3 in the ‘IN_ORDER’ array/list. All the elements to the left of 3 in the ‘IN_ORDER’ array/list will be part of the left subtree and all the elements to the right of 3 in ‘IN_ORDER’ will be part of the right subtree as highlighted above.
The number of elements on the left side of the ‘IN_ORDER’ array/list is 1 in our example. So, 1 element in the ‘POST_ORDER’ array/list from starting will be part of the left subtree and the next elements less than the root index will be part of the right subtree.
Here is the algorithm:
O(N) where ‘N’ is the number of nodes in the Binary Tree.
Because we are traversing the ‘POST_ORDER’ array/list and ‘IN_ORDER’ array/list only once. Hence the time complexity is O(N).
O(N) where ‘N’ is the number of nodes in the Binary Tree.
O(N) different function calls are stored in the stack space. So the space complexity is O(N).