Preorder to Postorder of BST
Given an array pre[] of N nodes representing preorder traversal of BST. The task is to print its postorder traversal.
Input:
The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N, the size of the array. The second line of each test case contains N input as pre[i].
Output:
Postorder traversal of the given preorder traversal is printed.
Examples:
    Input:	
    T = 1
    N = 8
    pre[120 90 96 105 240 270 300 360]
    
    Output: 
    105 96 90 360 300 270 240 120 
    Input:
    T = 1
    N = 5
    pre[80 60 70 160 200]
    
    Output: 
    70 60 200 160 80                                                                     
                            
Since we are given the preorder traversal of the tree, to construct any tree we need at least two traversal {inorder,preorder},{inorder,postorder},{inorder,levelorder} that is inorder is needed, but here only one traversal is given but one more important thing is the property of this tree, that is this tree is BST, which has its left child less than or equal to the root of the tree and right child as greater than the root element. So we will use this property and construct a BST and then we simply print the Postorder traversal of the tree.
Pseudo Code:
//postorder function to print data in postorder manner. void postorder(Node* root) { //if root==NULL then simply return if (root == NULL) return; else { postorder(root->left); //recursively call for left child postorder(root->right); //recursively call for right child cout << root->data << " "; //print the current data } return; } //function to construct binary search tree //from preorder traversal. void insert(ll data) { Node* newnode = new Node(data); //if root is NULL then create new node and assign it to root. if (root == NULL) { root = newnode; return; } else { Node* temp = root; while (1) { //if data is smaller than root element then it must in left //part of the BST so check for left side. if (temp->data > data) { //if left child of root is NULL then simply assign new node //to left part else left is assigned to //left child and then we check again and again //until we get correct structure. if (temp->left == NULL) { temp->left = newnode; break; } else temp = temp->left; } else { //if data is greater than equal to root element then //it must in right part of the BST so check for right side. //if right child of root is NULL then simply assign new node //to right part else right is assigned to //right child and then we check again and again until //we get correct structure. if (temp->right == NULL) { temp->right = newnode; break; } else temp = temp->right; } } } } // solve function which will return the BST tree and print it // postorder form after calling insert and postorder function. void solve(int pre[],int n) { // add the elements to BST for(int i=0;i<n;i++) insert(pre[i]); // print its postorder form. postorder(root); }C++ Implementation:
Output
need an explanation for this answer? contact us directly to get an explanation for this answer