Given a distinct sequence of keys which represents postorder traversal of a binary search tree, construct the tree from the postorder sequence.





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



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

Construct the root node of BST which would be the last key in the postorder sequence.

Find index i of the last key in the postorder sequence which is smaller than the root node.

Recur for right subtree with keys in the postorder sequence that appears after the i’th index (excluding last index).

Recur for left subtree with keys in the postorder sequence that appears before the i’th index (including i’th index).



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

The last item in the postorder sequence 15 becomes the root node.

Since 10 is the last key in the postorder sequence which smaller than the root node, left subtree consists of keys {8, 12, 10} and right subtree consists of keys {16, 25, 20} .

To construct the complete binary search tree, recursively repeat the above steps for postorder sequence {8, 12, 10} and {16, 25, 20} .



This is demonstrated below in C, Java and Python –

C #include <stdio.h> #include <stdlib.h> // Data structure to store a Binary Tree node struct Node { int key; struct Node *left, *right; }; // Function to create a new binary tree node having given key struct Node* newNode(int key) { struct Node* node = (struct Node*)malloc(sizeof(struct Node)); node->key = key; node->left = node->right = NULL; return node; } // Recursive function to perform inorder traversal of a binary tree void inorder(struct Node* root) { if (root == NULL) return; inorder(root->left); printf("%d ", root->key); inorder(root->right); } // Recursive function to build a binary search tree from // its postorder sequence struct Node* constructBST(int postorder[], int start, int end) { // base case if (start > end) return NULL; // Construct the root node of the subtree formed by keys of the // postorder sequence in range [start, end] struct Node *node = newNode(postorder[end]); // search the index of last element in current range of postorder // sequence which is smaller than the value of root node int i; for (i = end; i >=start; i--) { if (postorder[i] < node->key) break; } // Build right subtree before left subtree since the values are // being read from the end of the postorder sequence // recursively construct the right subtree node->right = constructBST(postorder, i + 1, end - 1); // recursively construct the left subtree node->left = constructBST(postorder, start, i); // return current node return node; } int main(void) { /* Construct below BST 15 / \ / \ 10 20 / \ / \ / \ / \ 8 12 16 25 */ int postorder[] = { 8, 12, 10, 16, 25, 20, 15 }; int n = sizeof(postorder)/sizeof(postorder[0]); // construct the BST struct Node* root = constructBST(postorder, 0, n - 1); // print the BST printf("Inorder Traversal of BST is : "); // inorder on the BST always returns a sorted sequence inorder(root); return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 #include <stdio.h> #include <stdlib.h> // Data structure to store a Binary Tree node struct Node { int key ; struct Node * left , * right ; } ; // Function to create a new binary tree node having given key struct Node * newNode ( int key ) { struct Node * node = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; node -> key = key ; node -> left = node -> right = NULL ; return node ; } // Recursive function to perform inorder traversal of a binary tree void inorder ( struct Node * root ) { if ( root == NULL ) return ; inorder ( root -> left ) ; printf ( "%d " , root -> key ) ; inorder ( root -> right ) ; } // Recursive function to build a binary search tree from // its postorder sequence struct Node * constructBST ( int postorder [ ] , int start , int end ) { // base case if ( start > end ) return NULL ; // Construct the root node of the subtree formed by keys of the // postorder sequence in range [start, end] struct Node * node = newNode ( postorder [ end ] ) ; // search the index of last element in current range of postorder // sequence which is smaller than the value of root node int i ; for ( i = end ; i >= start ; i -- ) { if ( postorder [ i ] < node -> key ) break ; } // Build right subtree before left subtree since the values are // being read from the end of the postorder sequence // recursively construct the right subtree node -> right = constructBST ( postorder , i + 1 , end - 1 ) ; // recursively construct the left subtree node -> left = constructBST ( postorder , start , i ) ; // return current node return node ; } int main ( void ) { /* Construct below BST 15 / \ / \ 10 20 / \ / \ / \ / \ 8 12 16 25 */ int postorder [ ] = { 8 , 12 , 10 , 16 , 25 , 20 , 15 } ; int n = sizeof ( postorder ) / sizeof ( postorder [ 0 ] ) ; // construct the BST struct Node * root = constructBST ( postorder , 0 , n - 1 ) ; // print the BST printf ( "Inorder Traversal of BST is : " ) ; // inorder on the BST always returns a sorted sequence inorder ( root ) ; return 0 ; } Download Run Code Output:



Inorder Traversal of BST is : 8 10 12 15 16 20 25

Java // Class to store a Binary Tree node class Node { int key; Node left, right; Node(int key) { this.key = key; } } class Main { // Recursive function to perform inorder traversal of a binary tree public static void inorder(Node root) { if (root == null) { return; } inorder(root.left); System.out.print(root.key + " "); inorder(root.right); } // Recursive function to build a binary search tree from // its postorder sequence public static Node constructBST(int[] postorder, int start, int end) { // base case if (start > end) { return null; } // Construct the root node of the subtree formed by keys of the // postorder sequence in range [start, end] Node node = new Node(postorder[end]); // search the index of last element in current range of postorder // sequence which is smaller than the value of root node int i; for (i = end; i >=start; i--) { if (postorder[i] < node.key) { break; } } // Build right subtree before left subtree since the values are // being read from the end of the postorder sequence // recursively construct the right subtree node.right = constructBST(postorder, i + 1, end - 1); // recursively construct the left subtree node.left = constructBST(postorder, start, i); // return current node return node; } public static void main(String[] args) { /* Construct below BST 15 / \ / \ 10 20 / \ / \ / \ / \ 8 12 16 25 */ int[] postorder = { 8, 12, 10, 16, 25, 20, 15 }; // construct the BST Node root = constructBST(postorder, 0, postorder.length - 1); // print the BST System.out.print("Inorder Traversal of BST is : "); // inorder on the BST always returns a sorted sequence inorder(root); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 // Class to store a Binary Tree node class Node { int key ; Node left , right ; Node ( int key ) { this . key = key ; } } class Main { // Recursive function to perform inorder traversal of a binary tree public static void inorder ( Node root ) { if ( root == null ) { return ; } inorder ( root . left ) ; System . out . print ( root . key + " " ) ; inorder ( root . right ) ; } // Recursive function to build a binary search tree from // its postorder sequence public static Node constructBST ( int [ ] postorder , int start , int end ) { // base case if ( start > end ) { return null ; } // Construct the root node of the subtree formed by keys of the // postorder sequence in range [start, end] Node node = new Node ( postorder [ end ] ) ; // search the index of last element in current range of postorder // sequence which is smaller than the value of root node int i ; for ( i = end ; i >= start ; i -- ) { if ( postorder [ i ] < node . key ) { break ; } } // Build right subtree before left subtree since the values are // being read from the end of the postorder sequence // recursively construct the right subtree node . right = constructBST ( postorder , i + 1 , end - 1 ) ; // recursively construct the left subtree node . left = constructBST ( postorder , start , i ) ; // return current node return node ; } public static void main ( String [ ] args ) { /* Construct below BST 15 / \ / \ 10 20 / \ / \ / \ / \ 8 12 16 25 */ int [ ] postorder = { 8 , 12 , 10 , 16 , 25 , 20 , 15 } ; // construct the BST Node root = constructBST ( postorder , 0 , postorder . length - 1 ) ; // print the BST System . out . print ( "Inorder Traversal of BST is : " ) ; // inorder on the BST always returns a sorted sequence inorder ( root ) ; } } Download Run Code Output:



Inorder Traversal of BST is : 8 10 12 15 16 20 25

Python # Class to store a Binary Tree node class Node: def __init__(self, key): self.key = key # Recursive function to perform inorder traversal of a binary tree def inorder(root): if root is None: return inorder(root.left) print(root.key, end=' ') inorder(root.right) # Recursive function to build a binary search tree from # its postorder sequence def constructBST(postorder, start, end): # base case if start > end: return None # Construct the root node of the subtree formed by keys of the # postorder sequence in range [start, end] node = Node(postorder[end]) # search the index of last element in current range of postorder # sequence which is smaller than the value of root node i = end while i >= start: if postorder[i] < node.key: break i = i - 1 # Build right subtree before left subtree since the values are # being read from the end of the postorder sequence # recursively construct the right subtree node.right = constructBST(postorder, i + 1, end - 1) # recursively construct the left subtree node.left = constructBST(postorder, start, i) # return current node return node if __name__ == '__main__': """ Construct below BST 15 / \ / \ 10 20 / \ / \ / \ / \ 8 12 16 25 """ postorder = [8, 12, 10, 16, 25, 20, 15] # construct the BST root = constructBST(postorder, 0, len(postorder) - 1) # print the BST print("Inorder Traversal of BST is: ", end='') # inorder on the BST always returns a sorted sequence inorder(root) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 # Class to store a Binary Tree node class Node : def __init__ ( self , key ) : self . key = key # Recursive function to perform inorder traversal of a binary tree def inorder ( root ) : if root is None : return inorder ( root . left ) print ( root . key , end = ' ' ) inorder ( root . right ) # Recursive function to build a binary search tree from # its postorder sequence def constructBST ( postorder , start , end ) : # base case if start > end : return None # Construct the root node of the subtree formed by keys of the # postorder sequence in range [start, end] node = Node ( postorder [ end ] ) # search the index of last element in current range of postorder # sequence which is smaller than the value of root node i = end while i >= start : if postorder [ i ] < node . key : break i = i - 1 # Build right subtree before left subtree since the values are # being read from the end of the postorder sequence # recursively construct the right subtree node . right = constructBST ( postorder , i + 1 , end - 1 ) # recursively construct the left subtree node . left = constructBST ( postorder , start , i ) # return current node return node if __name__ == '__main__' : """ Construct below BST 15 / \ / \ 10 20 / \ / \ / \ / \ 8 12 16 25 """ postorder = [ 8 , 12 , 10 , 16 , 25 , 20 , 15 ] # construct the BST root = constructBST ( postorder , 0 , len ( postorder ) - 1 ) # print the BST print ( "Inorder Traversal of BST is: " , end = '' ) # inorder on the BST always returns a sorted sequence inorder ( root ) Download Run Code Output:



Inorder Traversal of BST is : 8 10 12 15 16 20 25







The time complexity of above solution is O(n2) and takes O(n) extra space for the call stack. We can reduce the time complexity to O(n) by following a different approach that doesn’t involve searching for index which separates the keys of left and right subtree in postorder sequence.

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

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

Here’s C++, Java and Python program that demonstrates it:

C++ #include <iostream> #include <vector> #include <climits> using namespace std; // Data structure to store a Binary Tree node struct Node { int data; Node *left, *right; }; // Function to create a new binary tree node having given key Node* newNode(int key) { Node* node = new Node; node->data = key; node->left = node->right = nullptr; return node; } // Function to print the inorder traversal of a binary tree void inorder(Node* root) { if (root == nullptr) return; inorder(root->left); cout << root->data << ' '; inorder(root->right); } // Recursive function to build a binary search tree from // its postorder sequence Node* buildTree(vector<int> const &postorder, int& pIndex, int min, int max) { // Base case if (pIndex < 0) return nullptr; // Return if next element of postorder traversal from the end // is not in valid range int curr = postorder[pIndex]; if (curr < min || curr > max) return nullptr; // Construct the root node and decrement pIndex Node* root = newNode(curr); pIndex--; // Construct left and right subtree of the root node. // Build right subtree before left subtree since the values are // being read from the end of the postorder sequence // Since all elements in the right subtree of a BST must be greater // than the value of root node, set range as [curr+1..max] and recur root->right = buildTree(postorder, pIndex, curr+1, max); // Since all elements in the left subtree of a BST must be less // than the value of root node, set range as [min, curr-1] and recur root->left = buildTree(postorder, pIndex, min, curr-1); return root; } // Build a binary search tree from its postorder sequence Node* buildTree(vector<int> const &postorder) { // start from the root node (last element in postorder sequence) int postIndex = postorder.size() - 1; // set range of the root node as [INT_MIN, INT_MAX] and recur return buildTree(postorder, postIndex, INT_MIN, INT_MAX); } int main() { /* Construct below BST 15 / \ / \ 10 20 / \ / \ / \ / \ 8 12 16 25 */ // postorder traversal of BST vector<int> postorder = { 8, 12, 10, 16, 25, 20, 15 }; // construct the BST Node* root = buildTree(postorder); // print the BST cout << "Inorder Traversal of BST is : "; // inorder on the BST always returns a sorted sequence inorder(root); return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 #include <iostream> #include <vector> #include <climits> using namespace std ; // Data structure to store a Binary Tree node struct Node { int data ; Node * left , * right ; } ; // Function to create a new binary tree node having given key Node * newNode ( int key ) { Node * node = new Node ; node -> data = key ; node -> left = node -> right = nullptr ; return node ; } // Function to print the inorder traversal of a binary tree void inorder ( Node * root ) { if ( root == nullptr ) return ; inorder ( root -> left ) ; cout << root -> data << ' ' ; inorder ( root -> right ) ; } // Recursive function to build a binary search tree from // its postorder sequence Node * buildTree ( vector < int > const &postorder , int & pIndex , int min , int max ) { // Base case if ( pIndex < 0 ) return nullptr ; // Return if next element of postorder traversal from the end // is not in valid range int curr = postorder [ pIndex ] ; if ( curr < min || curr > max ) return nullptr ; // Construct the root node and decrement pIndex Node * root = newNode ( curr ) ; pIndex -- ; // Construct left and right subtree of the root node. // Build right subtree before left subtree since the values are // being read from the end of the postorder sequence // Since all elements in the right subtree of a BST must be greater // than the value of root node, set range as [curr+1..max] and recur root -> right = buildTree ( postorder , pIndex , curr + 1 , max ) ; // Since all elements in the left subtree of a BST must be less // than the value of root node, set range as [min, curr-1] and recur root -> left = buildTree ( postorder , pIndex , min , curr - 1 ) ; return root ; } // Build a binary search tree from its postorder sequence Node * buildTree ( vector < int > const &postorder ) { // start from the root node (last element in postorder sequence) int postIndex = postorder . size ( ) - 1 ; // set range of the root node as [INT_MIN, INT_MAX] and recur return buildTree ( postorder , postIndex , INT_MIN , INT_MAX ) ; } int main ( ) { /* Construct below BST 15 / \ / \ 10 20 / \ / \ / \ / \ 8 12 16 25 */ // postorder traversal of BST vector < int > postorder = { 8 , 12 , 10 , 16 , 25 , 20 , 15 } ; // construct the BST Node * root = buildTree ( postorder ) ; // print the BST cout << "Inorder Traversal of BST is : " ; // inorder on the BST always returns a sorted sequence inorder ( root ) ; return 0 ; } Download Run Code Output:



Inorder Traversal of BST is : 8 10 12 15 16 20 25

Java import java.util.concurrent.atomic.AtomicInteger; // class to store a Binary Tree node class Node { int data; Node left, right; Node(int data) { this.data = data; this.left = this.right = null; } } class Main { // Function to print the inorder traversal of a binary tree public static void inorder(Node root) { if (root == null) { return; } inorder(root.left); System.out.print(root.data + " "); inorder(root.right); } // Recursive function to build a binary search tree from // its postorder sequence public static Node buildTree(int[] postorder, AtomicInteger pIndex, int min, int max) { // Base case if (pIndex.get() < 0) { return null; } // Return if next element of postorder traversal from the end // is not in valid range int curr = postorder[pIndex.get()]; if (curr < min || curr > max) { return null; } // Construct the root node and decrement pIndex Node root = new Node(curr); pIndex.decrementAndGet(); // Construct left and right subtree of the root node. // Build right subtree before left subtree since the values are // being read from the end of the postorder sequence // Since all elements in the right subtree of a BST must be greater // than the value of root node, set range as [curr+1..max] and recur root.right = buildTree(postorder, pIndex, curr + 1, max); // Since all elements in the left subtree of a BST must be less // than the value of root node, set range as [min, curr-1] and recur root.left = buildTree(postorder, pIndex, min, curr - 1); return root; } // Build a binary search tree from its postorder sequence public static Node buildTree(int[] postorder) { // start from the root node (last element in postorder sequence) AtomicInteger postIndex = new AtomicInteger(postorder.length - 1); // set range of the root node as [Integer.MIN_VALUE, Integer.MAX_VALUE] and recur return buildTree(postorder, postIndex, Integer.MIN_VALUE, Integer.MAX_VALUE); } public static void main(String[] args) { /* Construct below BST 15 / \ / \ 10 20 / \ / \ / \ / \ 8 12 16 25 */ // postorder traversal of BST int[] postorder = { 8, 12, 10, 16, 25, 20, 15 }; // construct the BST Node root = buildTree(postorder); // print the BST System.out.print("Inorder Traversal of BST is : "); // inorder on the BST always returns a sorted sequence inorder(root); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 import java . util . concurrent . atomic . AtomicInteger ; // class to store a Binary Tree node class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; this . left = this . right = null ; } } class Main { // Function to print the inorder traversal of a binary tree public static void inorder ( Node root ) { if ( root == null ) { return ; } inorder ( root . left ) ; System . out . print ( root . data + " " ) ; inorder ( root . right ) ; } // Recursive function to build a binary search tree from // its postorder sequence public static Node buildTree ( int [ ] postorder , AtomicInteger pIndex , int min , int max ) { // Base case if ( pIndex . get ( ) < 0 ) { return null ; } // Return if next element of postorder traversal from the end // is not in valid range int curr = postorder [ pIndex . get ( ) ] ; if ( curr < min || curr > max ) { return null ; } // Construct the root node and decrement pIndex Node root = new Node ( curr ) ; pIndex . decrementAndGet ( ) ; // Construct left and right subtree of the root node. // Build right subtree before left subtree since the values are // being read from the end of the postorder sequence // Since all elements in the right subtree of a BST must be greater // than the value of root node, set range as [curr+1..max] and recur root . right = buildTree ( postorder , pIndex , curr + 1 , max ) ; // Since all elements in the left subtree of a BST must be less // than the value of root node, set range as [min, curr-1] and recur root . left = buildTree ( postorder , pIndex , min , curr - 1 ) ; return root ; } // Build a binary search tree from its postorder sequence public static Node buildTree ( int [ ] postorder ) { // start from the root node (last element in postorder sequence) AtomicInteger postIndex = new AtomicInteger ( postorder . length - 1 ) ; // set range of the root node as [Integer.MIN_VALUE, Integer.MAX_VALUE] and recur return buildTree ( postorder , postIndex , Integer . MIN_VALUE , Integer . MAX_VALUE ) ; } public static void main ( String [ ] args ) { /* Construct below BST 15 / \ / \ 10 20 / \ / \ / \ / \ 8 12 16 25 */ // postorder traversal of BST int [ ] postorder = { 8 , 12 , 10 , 16 , 25 , 20 , 15 } ; // construct the BST Node root = buildTree ( postorder ) ; // print the BST System . out . print ( "Inorder Traversal of BST is : " ) ; // inorder on the BST always returns a sorted sequence inorder ( root ) ; } } Download Run Code Output:



Inorder Traversal of BST is : 8 10 12 15 16 20 25

Python # class to store a Binary Tree node: class Node: def __init__(self, data, left=None, right=None): self.data = data self.left = left self.right = right # Function to print the inorder traversal of a binary tree def inorder(root): if root is None: return inorder(root.left) print(root.data, end=' ') inorder(root.right) # Recursive function to build a binary search tree from # its postorder sequence def buildBST(postorder, pIndex, min, max): # Base case if pIndex < 0: return None, pIndex # Return if next element of postorder traversal from the end # is not in valid range curr = postorder[pIndex] if curr < min or curr > max: return None, pIndex # Construct the root node and decrement pIndex root = Node(curr) pIndex = pIndex - 1 # Construct left and right subtree of the root node. # Build right subtree before left subtree since the values are # being read from the end of the postorder sequence # Since all elements in the right subtree of a BST must be greater # than the value of root node, set range as [curr+1..max] and recur root.right, pIndex = buildBST(postorder, pIndex, curr + 1, max) # Since all elements in the left subtree of a BST must be less # than the value of root node, set range as [min, curr-1] and recur root.left, pIndex = buildBST(postorder, pIndex, min, curr - 1) return root, pIndex # Build a binary search tree from its postorder sequence def buildTree(postorder): # start from the root node (last element in postorder sequence) postIndex = len(postorder) - 1 # set range of the root node as [float('-inf'), float('inf')] and recur return buildBST(postorder, postIndex, float('-inf'), float('inf'))[0] if __name__ == '__main__': """ Construct below BST 15 / \ / \ 10 20 / \ / \ / \ / \ 8 12 16 25 """ # postorder traversal of BST postorder = [8, 12, 10, 16, 25, 20, 15] # construct the BST root = buildTree(postorder) # print the BST print("Inorder Traversal of BST is : ", end='') # inorder on the BST always returns a sorted sequence inorder(root) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 # class to store a Binary Tree node: class Node : def __init__ ( self , data , left = None , right = None ) : self . data = data self . left = left self . right = right # Function to print the inorder traversal of a binary tree def inorder ( root ) : if root is None : return inorder ( root . left ) print ( root . data , end = ' ' ) inorder ( root . right ) # Recursive function to build a binary search tree from # its postorder sequence def buildBST ( postorder , pIndex , min , max ) : # Base case if pIndex < 0 : return None , pIndex # Return if next element of postorder traversal from the end # is not in valid range curr = postorder [ pIndex ] if curr < min or curr > max : return None , pIndex # Construct the root node and decrement pIndex root = Node ( curr ) pIndex = pIndex - 1 # Construct left and right subtree of the root node. # Build right subtree before left subtree since the values are # being read from the end of the postorder sequence # Since all elements in the right subtree of a BST must be greater # than the value of root node, set range as [curr+1..max] and recur root . right , pIndex = buildBST ( postorder , pIndex , curr + 1 , max ) # Since all elements in the left subtree of a BST must be less # than the value of root node, set range as [min, curr-1] and recur root . left , pIndex = buildBST ( postorder , pIndex , min , curr - 1 ) return root , pIndex # Build a binary search tree from its postorder sequence def buildTree ( postorder ) : # start from the root node (last element in postorder sequence) postIndex = len ( postorder ) - 1 # set range of the root node as [float('-inf'), float('inf')] and recur return buildBST ( postorder , postIndex , float ( '-inf' ) , float ( 'inf' ) ) [ 0 ] if __name__ == '__main__' : """ Construct below BST 15 / \ / \ 10 20 / \ / \ / \ / \ 8 12 16 25 """ # postorder traversal of BST postorder = [ 8 , 12 , 10 , 16 , 25 , 20 , 15 ] # construct the BST root = buildTree ( postorder ) # print the BST print ( "Inorder Traversal of BST is : " , end = '' ) # inorder on the BST always returns a sorted sequence inorder ( root ) Download Run Code Output:



Inorder Traversal of BST is : 8 10 12 15 16 20 25













(22 votes, average: 4.27 out of 5)

Loading...

Thanks for reading.