Given a distinct sequence of keys representing the postorder traversal of a binary search tree, construct a BST from it.

For example, the following BST should be constructed for postorder traversal {8, 12, 10, 16, 25, 20, 15}:

BST

Practice this problem

We can easily build a BST for a given postorder sequence by recursively repeating the following steps for all keys in it, starting from the right.

  1. Construct the root node of BST, which would be the last key in the postorder sequence.
  2. Find index i of the last key in the postorder sequence, which is smaller than the root node.
  3. Recur for right subtree with keys in the postorder sequence that appears after the i'th index (excluding the last index).
  4. Recur for left subtree with keys in the postorder sequence that appears before the i'th index (including i'th index).

Let’s consider the postorder traversal {8, 12, 10, 16, 25, 20, 15} to make the context more clear.

  1. The last item in the postorder sequence 15 becomes the root node.
  2. Since 10 is the last key in the postorder sequence, which smaller than the root node, the left subtree consists of keys {8, 12, 10} and the right subtree consists of keys {16, 25, 20}.
  3. To construct the complete binary search tree, recursively repeat the above steps for postorder sequence {8, 12, 10} and {16, 25, 20}.

The algorithm can be implemented as follows in C, Java, and Python:

C


Download  Run Code

Output:

Inorder traversal of BST is 8 10 12 15 16 20 25

Java


Download  Run Code

Output:

Inorder traversal of BST is 8 10 12 15 16 20 25

Python


Download  Run Code

Output:

Inorder traversal of BST is 8 10 12 15 16 20 25

The time complexity of the above solution is O(n2), where n is the size of the BST, and requires space proportional to the tree’s height for the call stack. We can reduce the time complexity to O(n) by following a different approach that doesn’t involve searching for an index that separates the left and right subtree keys in a postorder sequence.

 
We know that each node has a key that is greater than all keys present in its left subtree and less than the keys present in the right subtree in the BST. The idea to pass the information regarding the valid range of keys for the current root node and its children in the recursion itself.

We start by setting the range as [-INFINITY, INFINITY] for the root node. It means that the root node and any of its children can have keys ranging between -INFINITY and INFINITY. Like the previous approach, construct BST’s root node from the last item in the postorder sequence. Suppose the root node has value x, recur for the right subtree with range (x, INFINITY) and recur for the left subtree with range [-INFINITY, x). To construct the complete binary search tree, recursively set the range for each recursive call and return if the next element of the postorder traversal is out of the valid range.

Following is the C++, Java, and Python program that demonstrates it:

C++


Download  Run Code

Output:

Inorder traversal of BST is 8 10 12 15 16 20 25

Java


Download  Run Code

Output:

Inorder traversal of BST is 8 10 12 15 16 20 25

Python


Download  Run Code

Output:

Inorder traversal of BST is 8 10 12 15 16 20 25