Showing:
Handle Score
@Ibrahim Nash 5725
@akhayrutdinov 5111
@mb1973 4989
@Quandray 4908
@saiujwal13083 4156
@sanjay05 3762
@marius_valentin_dragoi 3511
@sushant_a 3453
@KshamaGupta 3318
@the_coder95 3302
Tree from Postorder and Inorder
Medium Accuracy: 49.98% Submissions: 16891 Points: 4

Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals.
For example, if given inorder and postorder traversals are {4, 8, 2, 5, 1, 6, 3, 7} and {8, 4, 5, 2, 6, 7, 3, 1}  respectively, then your function should construct below tree.

1
/      \
2        3
/    \     /   \
4     5   6    7
\
8

Example 1:

Input:
N = 8
in[] = 4 8 2 5 1 6 3 7
post[] =8 4 5 2 6 7 3 1
Output: 1 2 4 8 5 3 6 7
Explanation: For the given postorder and
inorder traversal of tree the  resultant
binary tree will be
1
/    \
2       3
/  \    /   \
4    5  6    7
\
8


Example 2:

Input:
N = 5
in[] = 9 5 2 3 4
post[] = 5 9 3 4 2
Output: 2 9 5 4 3
Explanation:  For the given postorder and
inorder traversal of tree the  resultant
binary tree will be
2
/      \
9        5
/   \
4      3

Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as inputs and returns the root node of the newly constructed Binary Tree. The pre order traversal of the returned node is printed by the driver's code.

Expected Time Complexity: O(N2).
Expected Auxiliary Space: O(N).

Constraints:
1 <= N <= 103
1 <= in[i], post[i] <= 103