Construct a BST from given postorder traversal using Stack
Given postorder traversal of a binary search tree, construct the BST.
For example,
1. If the given traversal is {1, 7, 5, 50, 40, 10}, then following tree should be constructed and root of the tree should be returned.
10
/ \
5 40
/ \ \
1 7 50
Input : 1 7 5 50 40 10
Output :
Inorder traversal of the constructed tree:
1 5 7 10 40 50If the given traversal is {2, 6, 4, 9, 13, 11, 7}, then following tree should be constructed and root of the tree should be returned.
7 / \ 4 11 / \ / \ 2 6 9 13Input : 2 6 4 9 13 11 7
Output :
Inorder traversal of the constructed tree:
2 4 6 7 9 11 13
Let us first see working Postorder traversal.
Left Right Root Hence last node of post order will be root of tree, create it and push to stack. If next element(i-1) is greater then it should be in right subtree. If next element(i-1) is less then it should be in left subtree.
Algorithm:
- Push root of the BST to the stack i.e, last element of the array.
- Start traversing the array in reverse, if next element is > the element at the top of the stack then,
set this element as the right child of the element at the top of the stack and also push it to the stack. - Else if, next element is < the element at the top of the stack then,
start popping all the elements from the stack until either the stack is empty or the current element becomes > the element at the top of the stack. - Make this element left child of the last popped node and repeat the above steps until the array is traversed completely.
Below is the implementation of the above algorithm.
Java
// Java implementation of the algorithm /* A binary tree node has data, pointer to left child and a pointer to right child */import java.util.*; // Class Node has data and references to the left // and the right child. class Node { int data; Node left, right; Node(int data) { this.data = data; left = right = null; } } class BinaryTree { // Function that creates the tree Node constructTreeUtil(int post[], int n) { // Last node is root Node root = new Node(post[n - 1]); Stack<Node> s = new Stack<>(); s.push(root); // Traverse from second last node for (int i = n - 2; i >= 0; --i) { Node x = new Node(post[i]); // Keep popping nodes while top() of stack // is greater. Node temp = null; while (!s.isEmpty() && post[i] < s.peek().data) temp = s.pop(); // Make x as left child of temp if (temp != null) temp.left = x; // Else make x as right of top else s.peek().right = x; s.push(x); } return root; } // Function that calls the method which contructs the tree Node constructTree(int post[], int size) { return constructTreeUtil(post, size); } // A utility function to print inorder traversal // of a Binary Tree void printInorder(Node node) { if (node == null) return; printInorder(node.left); System.out.print(node.data + " "); printInorder(node.right); } // Driver program to test above functions public static void main(String[] args) { BinaryTree tree = new BinaryTree(); int post[] = new int[] { 1, 7, 5, 50, 40, 10 }; int size = post.length; Node root = tree.constructTree(post, size); System.out.println("Inorder traversal of the constructed tree:"); tree.printInorder(root); } } |
C#
// C# implementation of the algorithm /* A binary tree node has data, pointer to left child and a pointer to right child */using System; using System.Collections.Generic; // Class Node has data and references // to the left and the right child. public class Node { public int data; public Node left, right; public Node(int data) { this.data = data; left = right = null; } } public class BinaryTree { // Function that creates the tree Node constructTreeUtil(int []post, int n) { // Last node is root Node root = new Node(post[n - 1]); Stack<Node> s = new Stack<Node>(); s.Push(root); // Traverse from second last node for (int i = n - 2; i >= 0; --i) { Node x = new Node(post[i]); // Keep popping nodes while top() of stack // is greater. Node temp = null; while (s.Count!=0 && post[i] < s.Peek().data) temp = s.Pop(); // Make x as left child of temp if (temp != null) temp.left = x; // Else make x as right of top else s.Peek().right = x; s.Push(x); } return root; } // Function that calls the // method which contructs the tree Node constructTree(int []post, int size) { return constructTreeUtil(post, size); } // A utility function to print // inorder traversal of a Binary Tree void printInorder(Node node) { if (node == null) return; printInorder(node.left); Console.Write(node.data + " "); printInorder(node.right); } // Driver code public static void Main() { BinaryTree tree = new BinaryTree(); int []post = new int[] { 1, 7, 5, 50, 40, 10 }; int size = post.Length; Node root = tree.constructTree(post, size); Console.WriteLine("Inorder traversal of the constructed tree:"); tree.printInorder(root); } } /* This code contributed by PrinciRaj1992 */ |
Inorder traversal of the constructed tree: 1 5 7 10 40 50
Recommended Posts:
- Iterative Postorder Traversal | Set 2 (Using One Stack)
- Find postorder traversal of BST from preorder traversal
- Construct a Binary Search Tree from given postorder
- Iterative Postorder traversal | Set 3
- Iterative Postorder Traversal | Set 1 (Using Two Stacks)
- Construct BST from given preorder traversal | Set 1
- Construct BST from given preorder traversal | Set 2
- Construct BST from its given level order traversal
- If you are given two traversal sequences, can you construct the binary tree?
- Inorder Tree Traversal without recursion and without stack!
- Level order traversal in spiral form | Using one stack and one queue
- Stack Permutations (Check if an array is stack permutation of other)
- Infix to Postfix using different Precedence Values for In-Stack and Out-Stack
- Find maximum in stack in O(1) without using additional stack
- Stack | Set 3 (Reverse a string using stack)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : princiraj1992



