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