Why this recursive sol is only partially correct ,help me out!


#1

int c=0;
TreeNode* help(vectorpre,vectorin,int s,int e){
if(s>e)return NULL;
TreeNode *temp=new TreeNode(pre[c++]);
if(s==e)return temp;
int index=0;
for(int i=0;i<in.size();i++){
if(in[i]==temp->val)index=i;
}
temp->left=help(pre,in,s,index-1);
temp->right=help(pre,in,index+1,e);
return temp;

}
TreeNode* Solution::buildTree(vector &A, vector &B) {
c=0;
return help(A,B,0,A.size()-1);
}


#2

Pass the vectors by reference.
Passing by value causes creation of multiple copies of vector which consumes time and memory.


#3

you should not use global variables