```
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if(preorder.empty()||inorder.empty())return NULL;
int rootVal=preorder.front();
preorder.erase(preorder.begin());
auto rootPos=find(inorder.begin(),inorder.end(),rootVal);
TreeNode*root=new TreeNode(rootVal);
vector<int> inorder_l(inorder.begin(),rootPos);
vector<int> inorder_r(rootPos+1,inorder.end());
root->left=buildTree(preorder,inorder_l);
root->right=buildTree(preorder,inorder_r);
return root;
}
};
```