Not Simple Ques Solution using Recursion


#1
int search(vector<int>& in,int start,int end,int no)
{
    for(int i=start;i<=end;i++)
    {
        if(in[i]==no)
        return i;
    }
    return -1;
}

// int k = 0;
TreeNode* make(vector& in,vector& pre,int start,int end,int &preindex)
{

    if(start>end)
    return NULL;
    
    TreeNode * node  = new TreeNode(pre[preindex]);
    int  no = node->val;
    preindex++;
    
    if(start==end)
    return node;
    
    int pos = search(in,start,end,no);
    
    node->left=make(in,pre,start,pos-1,preindex);
    node->right= make(in,pre,pos+1,end,preindex);
    
    return node;
}

TreeNode* Solution::buildTree(vector &pre, vector &in) {

    int k=0;
    
    int n = pre.size();
    TreeNode* root = make(in,pre,0,n-1,k);
    
    //vector<int>ans;
    return root;

}