Error in lower_bound function

programming
interview-questions
Tags: #<Tag:0x00007f2428ebc3c0> #<Tag:0x00007f2428ebc280>

#1

int glob=0;
TreeNode* func(vector &pre,vector &in,int a,int b){
if(a>b){
return NULL;
}
TreeNode ans=new TreeNode(pre[glob]);
int i=lower_bound(in.begin(),in.end(),pre[glob])-in.begin();
// int i=a;
// while(in[i]!=pre[glob]){
// i++;
// }
glob++;
ans->left=func(pre,in,a,i-1);
ans->right=func(pre,in,i+1,b);
return ans;
}
TreeNode
Solution::buildTree(vector &A, vector &B) {
glob=0;
TreeNode *ans;
ans=func(A,B,0,A.size()-1);
return ans;
}

Here in the function “func” when I use the simple while loop to find the value of “i”, then the programs runs fine.
But when I use the lower_bound statement which calculates the value of “i” using binary search the loop runs infinitely.
I have manually checked many times but I am not able to understand why the lower_bound part in the func function is causing error.