Tree from Postorder and Inorder | Practice | GeeksforGeeks

Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals.
 
Example 1:
Input:
N = 8
in[] = 4 8 2 5 1 6 3 7
post[] =8 4 5 2 6 7 3 1
Output: 1 2 4 8 5 3 6 7
Explanation: For the given postorder and
inorder traversal of tree the  resultant
binary tree will be
           1
       /      \
     2         3
   /  \      /  \
  4    5    6    7
   \
     8

 
Example 2:
Input:
N = 5
in[] = 9 5 2 3 4
post[] = 5 9 3 4 2
Output: 2 9 5 4 3
Explanation:  
the  resultant binary tree will be
           2
        /     \
       9       4
        \     /
         5   3
 
Your Task:
You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree.
The generated output contains the preorder traversal of the constructed tree.
 
Expected Time Complexity: O(N2)
Expected Auxiliary Space: O(N)
 
Constraints:
1 <= N <= 103
1 <= in[i], post[i] <= 103
 
0
rajeshsurana1 hour ago
Java recursive solution from hint video 
    Node buildTree(int in[], int fi, int li, int post[], int fp, int lp) {
        if (fi > li || fp > lp) return null;
        int rootVal = post[lp];
        Node root = new Node(rootVal);
        int i = fi;
        for (; i<=li; i++) {
            if (in[i]==rootVal) break;
        }
        int leftSize = i-fi;
        root.left = buildTree(in, fi, i-1, post, fp, fp+leftSize-1);
        root.right = buildTree(in, i+1, li, post, fp+leftSize, lp-1);
        return root;
    }
    //Function to return a tree created from postorder and inoreder traversals.
    Node buildTree(int in[], int post[], int n) {
        
        return buildTree(in, 0, in.length-1, post, 0, post.length-1);
    }
}
-1
subhamk473 weeks ago
class GfG{   //Function to return a tree created from postorder and inoreder traversals.   int index = 0;   Node buildTree(int in[], int post[], int n) {       index = n-1;       return helper(in , post,0,n-1);   }   Node helper(int in[], int post[],int start, int end){       if(start>end) return null;       Node head = new Node(post[index--]);       int nInd = start;       for(int i = start;i<=end;i++){           if(in[i]==head.data) {               nInd = i;               break;           }       }       head.right= helper(in, post,nInd+1, end);       head.left = helper(in, post,start, nInd-1);       return head;   }} 
0
amishasahu3283 weeks ago
map<int, int> mp;
Node* BT(int inorder[], int inStart, int inEnd, int postorder[], int postStart, int postEnd)
{
        if(inStart > inEnd || postStart > postEnd) return NULL;
        Node *root = new Node(postorder[postEnd]);
        int idx = mp[root->data];
        int leftNodes = idx - inStart;
        
        root->left = BT(inorder, inStart, idx-1, postorder, postStart, postStart+leftNodes-1);
        root->right = BT(inorder, idx+1, inEnd, postorder, postStart+leftNodes, postEnd-1);
        
        return root;
}
Node *buildTree(int in[], int post[], int n) {
    // Your code here
    for(int i = 0; i < n; i++)
            mp[in[i]] = i;
        
    return BT(in, 0, n-1, post, 0, n-1);
}

0
yasirkhan2012371 month ago
Node *buildTree(int in[], int post[], int n) {   // Your code here   Node*root= new Node(post[n-1]);   if(n==0){       return NULL;   }   int i = 0;   while(in[i]!=post[n-1]){       i++;   }   int leftsize = i;   int rightsize = n-i-1;   Node * lefttree = buildTree(in,post,leftsize);   Node * righttree = buildTree(in+leftsize+1,post+leftsize,rightsize);      root->left = lefttree;   root->right = righttree;   return root;}
0
aniketkumar51 month ago
  Node buildTree(int in[], int post[], int n) {       // Your code here     if(n <= 0)       return null;      Node root = new Node(post[n-1]);   int i;   for(i=0;i<n;i++)   {       if(post[n-1] == in[i])           break;   }   int[] in1=new int[n-i+1];    int[] post1=new int[n-i];   for(int k=0,l=i+1;k<n && l<n;k++,l++){            in1[k]=in[l];     }    for(int k=0,l=i;k<n && l<n;k++,l++){            post1[k]=post[l];     }   root.right = buildTree(in1, post1, n-1-i);   root.left = buildTree(in, post, i);   return root;   }
0
muskansharma06m2 months ago
Node *build(int in[],int post[],int start,int end, int &idx)
{
   if(start>end)
       return NULL;
   int curr=post[idx--];
   Node* node = new Node(curr);
   if(start==end)
       return node;
   int pos=-1;
   for(int i=start;i<=end;i++)
   {
       if(in[i]==curr)
           pos=i;
   }
   node->right=build(in,post,pos+1,end,idx);
   node->left=build(in,post,start,pos-1,idx);
   return node;
}
Node *buildTree(int in[], int post[], int n) {
   int idx=n-1;
   return build(in,post,0,n-1,idx);
   // Your code here
}

+1
ranjit95s2 months ago
void creatingMapping(int in[], map<int,int> &nodeToIndex, int n)
{
    for(int i = 0; i < n; i++)
        nodeToIndex[in[i]] = i;
}

Node* solve(int in[], int post[],int &index,int inOS, int inOE, int n, map<int,int> &nodeToIndex)
{
    if(index < 0 || inOS > inOE)
        return NULL;
        
    int elem = post[index--];
    Node* root = new Node(elem);
    int pos = nodeToIndex[elem];
    
    root -> right = solve(in, post, index, pos + 1, inOE, n, nodeToIndex);
    
    root -> left = solve(in, post, index, inOS, pos - 1, n, nodeToIndex);
    
    return root;
}

//Function to return a tree created from postorder and inoreder traversals.
Node *buildTree(int in[], int post[], int n) {
    int postOrderIndex = n - 1;
    
    map<int,int> nodeToIndex;
    creatingMapping(in,nodeToIndex,n);
    
    Node* ans = solve(in,post,postOrderIndex,0,n-1,n,nodeToIndex);
    
    return ans;
}
+6
ntt4541912 months ago
C++ 0.0 pointer arithmetic
Node *buildTree(int in[], int post[], int n) {
    if(n<=0) return nullptr;
    
    Node* root= new Node(post[n-1]);
    int i;
    for(i=0; i<n; i++) if(post[n-1]==in[i]) break;
    
    root->left= buildTree(in,post,i);
    root->right=buildTree(in+i+1,post+i,n-i-1);
    return root;
}
0
siddharthguptamin202 months ago
Caan anyone help??
 
int postindex=0;Node *ctree(int in[],int post[],int is,int ie,int n){   if(is>ie)   return NULL;   Node *root=new Node(post[n-1-postindex++]);   int index;   for(int i=is;i<=ie;i++)   {       if(in[i]==root->data)       {index=i;break;}   }   root->right=ctree(in,post,index+1,ie,n);   root->left=ctree(in,post,is,index-1,n);   return root;}Node *buildTree(int in[], int post[], int n) {   // Your code here         return ctree(in,post,0,n-1,n);   } 
0
ashwinkumar99312 months ago
Node *build(int in[], int post[], int s, int e){      if(s>e){       return NULL;   }      int index = -1;   for(int i=e;i>=0;i--){       for(int j=e;j>=s;j--){           if(in[j] == post[i]){               index = j;               break;           }       }       if(index!=-1){           break;       }   }   Node* root = new Node(in[index]);   root->left = build(in, post, s, index-1);   root->right = build(in, post, index+1, e);   return root;}
Node *buildTree(int in[], int post[], int n) {   return build(in, post, 0, n-1);} 
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.