class Solution {
public:
TreeNode* buildTree(vector<int> preorder, vector<int> inorder) {
TreeNode* root=helper(preorder,inorder,0,(preorder.size()-1));
return root;
}
int Search(vector<int>inorder,int strt,int end,int val)
{
int i;
for(i=strt;i<=end;i++){
if(inorder[i]==val){break;}
}return i;
}
TreeNode* helper(vector<int>preorder,vector<int>inorder,int strt,int end)
{
if(strt>end){return NULL;}
static int index=0;
TreeNode* root=new TreeNode(preorder[index++]);
if(strt==end){return root;}
int bound=Search(inorder,strt,end,root->val);
root->left=helper(preorder,inorder,strt,bound-1);
root->right=helper(preorder,inorder,bound+1,end);
return root;
}
};