


• The left subtree of a node contains only nodes with data less than the node’s data.
• The right subtree of a node contains only nodes with data greater than the node’s data.
• Both the left and right subtrees must also be binary search trees.
For the given post order traversal: 2 4 3 7 6 5
The BST will be:
The Inorder Traversal of this BST is 2 3 4 5 6 7.
The first line of the input contains a single integer 'T', representing the number of test cases.
The first line of each test case contains an integer 'N', which denotes the number of nodes in the tree.
The second line of each test case contains ‘N’ single space-separated integers denoting the post-order traversal of the tree.
For each test case, print the inorder traversal of the BST.
Print the output of each test case in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
0 <= postOrder[i] <= 10^5
Time Limit: 1 sec
The basic idea is to find the position of every node in the tree. The last element of the post order is always the root of the tree. The elements smaller than root should be present in the left subtree, and elements greater than root should be present in the right subtree to satisfy BST conditions. So, we find the last element index, which is smaller than the root, and divide the array to build the BST.
Here is the algorithm :
HELPER(‘postORDER’, ‘LOW’, ‘HIGH’) (where ‘postOrder’ is the given array, ‘LOW’ and ‘HIGH’ are indexes of the array initialized from 0 to ‘N’ - 1).
We maintain a range for each node such that the value of that node should lie between the range. The range for ‘ROOT’ can be initialized with a small number and with a large number. For a node in the subtree, we update the range by setting its max value to be data of ‘ROOT’ - 1 for the left subtree, whereas the min value for the right subtree would be data of ‘ROOT’ + 1.
Here is the algorithm :
HELPER(‘postORDER’, ‘IDX’, ‘minDATA’, ‘maxDATA’) (where ‘postOrder’ is the given array, ‘IDX’ is the index for traversing initialized with ‘N’ - 1, ‘minDATA’, and ‘maxDATA’ is the range of a node).
Sorted Doubly Linked List to Balanced BST
Longest Substring with K-Repeating Characters
Expression Add Operators
Gray Code Transformation
Count of Subsequences with Given Sum