class Solution { private: TreeNode* generateTree(int preStart, int inStart, int inEnd, vector<int>& preorder, vector<int>& inorder) { if(preStart > preorder.size()1  inStart > inEnd) return NULL; TreeNode* root = new TreeNode(preorder[preStart]); int inIndex = 0; for(int i = inStart; i <= inEnd; i++) { if(inorder[i] == preorder[preStart]){ inIndex = i; break; } } root>left = generateTree(preStart+1, inStart, inIndex1, preorder, inorder); root>right = generateTree(preStart+inIndexinStart+1, inIndex+1, inEnd, preorder, inorder); return root; } public: TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) { return generateTree(0, 0, inorder.size()1, preorder, inorder); } };
