I don't get it! What is wrong with my recursive solution


#1

// Here is my code is failing for some test cases don’t know why
int search(vector &B,int s,int e,int t) {
for(int j=s;j<=e;j++) {
if(B[j]==t) {
return j;
}
}
return -1;
}
TreeNode* build(vector &A, vector &B,int s, int e) {
static int i = 0;
if(s>e && i>=A.size()) {
return NULL;
}
TreeNode* root = new TreeNode(A[i++]);
if(s==e) {
return root;
}
int index = search(B,s,e,root->val);
root->left = build(A,B,s,index-1);
root->right = build(A,B,index+1,e);
return root;
}
TreeNode* Solution::buildTree(vector &A, vector &B) {
return build(A,B,0,A.size()-1);
}


#2

Here A is preorder vector and B is inorder do not get confused.