Construct BST from Postorder | Practice | GeeksforGeeks

Given postorder traversal of a Binary Search Tree, you need to construct a BST from postorder traversal. The output will be inorder traversal of the constructed BST.
 
Example 1:
Input:
6
1 7 5 50 40 10

Output:
1 5 7 10 40 50

Explanation:
Testcase 1: The BST for the given post order traversal is:


Thus the inorder traversal of BST is: 1 5 7 10 40 50.
 
Your Task:
The task is to complete the function constructTree() which takes an array post[], size as as the argument and returns the root of BST. 
 
Expected Time Complexity: O(Height of the BST)
Expected Auxiliary Space: O(Height of the BST)
 
Constraints:
1 <= T <= 100
1 <= N <= 100
0
jerry2331 week ago
22 
134 37 369 334 304 249 499 190 530 568 747 649 755 754 809 529 891 959 909 873 869 843 
 
Can someone tell me how is this a postorder traversal of a Binary Search Tree? Isn't the first element in a postorder traversal of a BST is the smallest element?
0
linhnvhan1 month ago
Node *constructTree (int post[], int size){//code here   if (size == 0)       return nullptr;   stack<Node*> st;   Node* root = new Node (post[size - 1]);   Node* p = root;   st.push (root);      for (int i = size - 2; i >= 0; --i) {       Node* node = new Node (post[i]);       if (post[i] > p->data) {           st.push (node);           p->right = node;           p = node;       } else {           while (!st.empty() && st.top()->data > post[i]) {               p = st.top();               st.pop();           }           p->left = node;           st.push (node);           p = node;       }   }      return root;}
0
cshubham4391 month ago
EASY JAVA SOUTION 
static Node root;
public static Node construct(int arr[], int start, int end){
    if(start>end)
    return null;
    int mid = (start+end)/2;
    Node node = new Node(arr[mid]);
    node.left = construct(arr,start,mid-1);
    node.right = construct(arr,mid+1,end);
    return node;
}
    public static Node constructTree(int post[],int n)
    {
        //Add your code here.
        java.util.Arrays.sort(post);
        root = construct(post,0,n-1);
        return root;
0
jaatgfg111 month ago
Node* inorderbst(Node *root1,int v[],int s,int e){       if(s>e){           return NULL;       }       int mid=(s+e)/2;       root1=new Node(v[mid]);       root1->left=inorderbst(root1->left,v,s,mid-1);       root1->right=inorderbst(root1->right,v,mid+1,e);       return root1;       }Node *constructTree (int post[], int size){ sort(post,post+size); Node* root=NULL; int s=0; int e=size-1; return inorderbst(root,post,s,e);}
0
aasif23642 months ago
Node *helper(int *post , int &postIdx , int key , int minVal , int maxVal){   if(postIdx < 0){       return NULL;   }      if(key < minVal or key > maxVal){       return NULL;   }      Node *root= new Node(key);   postIdx-=1;   if(postIdx >= 0){       root->right=helper(post , postIdx , post[postIdx] , key , maxVal);   }      if(postIdx >= 0){       root->left=helper(post , postIdx , post[postIdx] , minVal , key);   }      return root;}Node *constructTree (int post[], int size){   int idx=size-1;   return helper(post , idx , post[idx] , INT_MIN , INT_MAX);}
0
sksbgp992 months ago
Quick and easy solution....C++
Node * constructT(int post[], int s, int e){   if(s>e)    {        return NULL;    }   int m=(s+e)/2;   Node * root=new Node(post[m]);   root->left=constructT(post, s, m-1);   root->right=constructT(post,m+1, e);   return root;   }Node *constructTree (int post[], int size){//code here sort(post,post+size); Node * root=constructT(post,0,size-1); return root;}
+1
akashshuklaaio3 months ago
Node *constructTree (int post[], int size){   if(size<=0){       return NULL;   }   Node* root=new Node(post[size-1]);       int i=0;   while(i<size-1 && post[size-1]>post[i]){       i++;   }      root->left=constructTree(post+0,i);   root->right=constructTree(post+i,size-i-1);      return root;   }
0
ileon3 months ago
Python
 
class Solution:
   
   def constructTree(self,post,n):
       # code here
       if len(post) == 0:
           return
       post.sort()
       for x in post:
           print(x, end=" ")
+1
dawsonquadros3 months ago
Recursive Solution:
Time Taken : 0.0/1.1
Node* Tree(int in[] , int left , int right , int post[] , int lb , int rb){
    if(left > right || lb > rb)return NULL;
    Node* root = new Node(post[rb]);
    
    int pivot = left;
    while(root->data != in[pivot])pivot++;
    
    root->left = Tree(in , left , pivot-1 , post , lb , lb+pivot-left-1);
    root->right = Tree(in , pivot+1 , right , post , lb+pivot-left , rb-1);
    
    return root;
}

Node *constructTree (int post[], int size){
    int in[size];
    for(int i = 0 ; i < size ; i++){
        in[i] = post[i];
    }
    sort(in , in+size);
    return Tree(in , 0 , size-1 , post , 0 , size-1);
}
0
aniketaggarwal123454 months ago
class GFG{   public static Node constructTree(int post[],int n)   {       //Add your code here.       idx = n-1;       return construct(post , Integer.MIN_VALUE , Integer.MAX_VALUE);   }   static int idx;   public static Node construct(int[] post , int min , int max)   {       if(idx < 0 || post[idx] < min || post[idx]> max)       {           return null;       }       int val = post[idx];       Node node = new Node(val);       idx--;              node.right = construct(post , val + 1 , max);       node.left = construct(post , min , val - 1);       return node;   }}
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.