Preorder to Postorder | Practice | GeeksforGeeks

Given an array arr[] of N nodes representing preorder traversal of BST. The task is to print its postorder traversal.
In Pre-Order traversal, the root node is visited before the left child and right child nodes.
Post-order traversal is one of the multiple methods to traverse a tree.
Example 1:
Input:
N = 5
arr[]  = {40,30,35,80,100}
Output: 35 30 100 80 40
Explanation: PreOrder: 40 30 35 80 100
InOrder: 30 35 40 80 100
Therefore, the BST will be:
              40
           /      \
         30       80
           \        \   
           35      100
Hence, the postOrder traversal will
be: 35 30 100 80 40
Example 2:
Input:
N = 8
arr[]  = {40,30,32,35,80,90,100,120}
Output: 35 32 30 120 100 90 80 40
Your Task:
You need to complete the given function and return the root of the tree. The driver code will then use this root to print the post order traversal.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= N <= 103
1 <= arr[i] <= 104
+2
shubhamxthakur2 weeks ago
Recursive C++ solution (No extra space except recursive call stack)
 
Node* construct(int arr[], int l, int h){
    if(l>h) return NULL;
    Node* root = newNode(arr[l]);
    int i=l+1;
    while(arr[i]<arr[l] && i<=h) i++;
    root->left = construct(arr, l+1, i-1);
    root->right = construct(arr, i, h);
    return root;
}

Node* post_order(int pre[], int n)
{
    if(n==0) return NULL;
    return construct(pre, 0, n-1);
}
-1
sangamchoudhary72 months ago
Node* solve(int pre[],int &i,int ub,int n){
    if(i >= n or pre[i] >= ub) return NULL;
    
    Node* root = newNode(pre[i++]);
    
    root->left = solve(pre,i,root->data,n);
    root->right = solve(pre,i,ub,n);
    
    return root;
}

Node* post_order(int pre[], int n){
    int i=0;
    return solve(pre,i,INT_MAX,n);
}
+1
aditya202 months ago
Solution using Stack  ( time - O ( n ) and Space O ( height of tree ) )

 if(size == 0)
       return NULL;
   if(size == 1){
       Node *re = newNode(pre[0]);
       return re;
   }
   // // changing the array into node tree
   // // Node *P = newNode(pre[0]);
   vector<Node*> v;
   for(int i=0;i<size;i++){
       Node *temp = newNode(pre[i]);
       v.push_back(temp);
   }
   stack<Node*> S;
   Node *P = v[0];
   S.push(P);
   Node *re = P;
   for(int  i = 1;i < size ; i++){
       
       while(!S.empty() && S.top()->data < v[i]->data){
           P = S.top();
           S.pop();
       }
       if(P->data < v[i]->data){
           
           P->right = v[i];
           
       }else{
               
           P->left = v[i];
           
       }
       S.push(v[i]);
           P = S.top();
   }
   return re;
+4
srivasaurabh792 months ago
Striver Approach 
 
int i = 0;
Node* build(int pre[] , int n , int mxval)
{
   if(i == n || pre[i] > mxval)
   {
       return NULL;
   }
   
   Node* root = newNode(pre[i++]);
   
   root->left = build(pre , n , root->data);
   root->right = build(pre , n , mxval);
   return root;
}
Node* post_order(int pre[], int size)
{
   i = 0;
   return build(pre , size , INT_MAX);
}
+1
spsahilpandey3 months ago
Node* solve(int a[],int l,int r){   if(l>r) return NULL;   Node *temp=newNode(a[l]);   if(l==r) return temp;   int p=l+1;   while(p<=r && a[p]<a[l]) p++;   temp->left=solve(a,l+1,p-1);   temp->right=solve(a,p,r);}// vector<int> res;// void post(Node* root){//     if(!root) return;//     post(root->left);//     post(root->right);//     res.push_back(root->data);// }Node* post_order(int pre[], int size){   //code here   return solve(pre,0,size-1);   }
-1
shashikantg3 months ago
Node * treemaker(int * pre,int size, int& idx, int minR, int maxR){
    if(idx==size || pre[idx]<minR || pre[idx]>maxR)
    return NULL;
    int curr=pre[idx++];
    Node * root=newNode(curr);
    root->left=treemaker(pre, size, idx, minR, curr);
    root->right=treemaker(pre, size, idx, curr, maxR);
    return root;
}

Node* post_order(int pre[], int size)
{
    int idx=0;
    return treemaker(pre, size, idx, INT_MIN, INT_MAX);
}
-1
vishalpandey100220003 months ago
public static Node post_order(int pre[], int size) {   //Your code here   Node root=null;       for (int i=0;i<size;i++){           root=preo(root,pre[i]);       }       return root;}private static Node preo(Node root, int i) {       if (root==null){           return new Node(i);       }       if (root.data>i){           root.left=preo(root.left,i);       }       else{           root.right=preo(root.right,i);       }       return root;   } 
-1
siddhant073 months ago
Node* makeTree(Node *root, int x){
    if(root == NULL){
        return newNode(x);
    }
    if(x < root->data){
        root->left = makeTree(root->left, x);
    }
    else{
        root->right = makeTree(root->right, x);
    }
    return root;
}


Node* post_order(int pre[], int size)
{
    Node *root = NULL;
    for(int i = 0; i < size; i++){
        root = makeTree(root, pre[i]);
    }
    return root;
}
0
abhixhek053 months ago

Node* func(int arr[], int l, int r){

    if(l<=r){
        Node *root = newNode(arr[l]);
        int mid = l+1;
        
    while(mid<=r && arr[mid]<arr[l] ) mid++;
        
     root->left = func(arr,l+1,mid-1);
    root->right = func(arr,mid,r);
        
        return root;
    }
    
    return NULL;
    
}

//Function that constructs BST from its preorder traversal.
Node* post_order(int pre[], int size)
{
    //code here
    return func(pre, 0, size-1);
    
}
0
absi20cs3 months ago
EASIEST C++ SOLUTION 0.0/1.1
Node* post_order(int pre[], int size){   //code here   Node *root=newNode(pre[0]);   Node *p,*t;   for(int i=1;i<size;i++)   {       t=root;           while(t!=NULL)           {               p=t;               if(pre[i]<t->data)               t=t->left;               else               t=t->right;           }           if(p->data>pre[i])           p->left=newNode(pre[i]);           else           p->right=newNode(pre[i]);          }   return root;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.