Construct a Full Binary Tree | Practice | GeeksforGeeks

Given two arrays that represent Preorder traversals of a Full binary tree preOrder[] and its mirror tree preOrderMirror(), your task is to complete the function constructBinaryTree(), that constructs the full binary tree using these two Preorder traversals.
Examples:
Input :  preOrder[] = {1,2,4,5,3,6,7}
         preOrderMirror[] = {1,3,7,6,2,5,4}

Output :          1
               /    \
              2      3
            /   \   /  \
           4     5 6    7

Note: It is not possible to construct a general binary tree using these two traversal. But it is possible to create a full binary tree using the above traversals without any ambiguity. For more details refer to this article.
Input:
The function takes three arguments as input, first the array that represent Preorder traversals of a Full binary tree preOrder[], second the array that represents the preorder traversal of its mirror tree preOrderMirror() and last the size of both the array.
There will be T test cases and for each test case the function will be called separately.

Output:
For each test case the function must return the root to the new binary tree formed. Ouput will be the inorder traversal of the binary tree.

Constraints:
1<=T<=500
1<=N<=103

Example:
Input:
2
3
0 1 2 
0 2 1
7
1 2 4 5 3 6 7
1 3 7 6 2 5 4
Output:
1 0 2 
4 2 5 1 6 3 7 
 
+1
harendraseervi1234567892 months ago
Mirror is Not Needed 😅
 
int i=0;Node*solve(int pre[],int temp,int lev,int n){   if(i==n) return NULL;   Node*root=new Node(pre[i++]);   if(temp==lev) return root;   root->left=solve(pre,temp+1,lev,n);   root->right=solve(pre,temp+1,lev,n);   return root;}Node* constructBinaryTree(int pre[], int preMirror[], int size){   i=0;   int lev=log2(size+1);   return solve(pre,1,lev,size);}
 
0
rajat25gupta6 months ago
Solution In Java : 
 
static class INT{       int a;       INT(int a){           this.a = a;       }   }   public Node constructBTree(int pre[], int preM[], int size)   {       // your code here       INT preIndex = new INT(0);       return constructBTreeUtil(pre, preM, preIndex, 0, size - 1, size);   }   static Node constructBTreeUtil(int[] pre, int[] preM, INT preIndex, int l, int  h, int size){       if(preIndex.a >= size || l>h)           return null;       Node root = new Node(pre[preIndex.a]);       ++(preIndex.a);       if(l == h)           return root;       int i;       for(i=l; i<=h; i++)           if(pre[preIndex.a] == preM[i])               break;       root.left = constructBTreeUtil(pre, preM, preIndex, i, h, size);       root.right = constructBTreeUtil(pre, preM, preIndex, l+1, i-1, size);       return root;   }   
+1
anutiger6 months ago
Node* constructBinaryTree(int pre[], int preMirror[], int size)
{
    // Code here
    if(size == 0)
        return  NULL;
    map<int,int> mp;
    Node *root  =  new Node(pre[0]);
    stack<Node*> st;
    st.push(root);
    for(int i = 0; i < size ; i ++){
        mp[preMirror[i]] = i;
    }
    int i = 1;
    while(i < size){
        int data = pre[i];
        Node *res = new  Node(data);
        while(!st.empty()){
            Node *temp = st.top();
            if(mp[temp->data] < mp[data]){
                if(!temp->left){
                    temp->left = res;
                    st.push(res);
                }
                else{
                    temp->right  =res;
                    st.pop();
                    st.push(res);
                }
                break;
            }
            else{
                st.pop();
            }
        }
        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.