1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
class Solution { public: unordered_map<int, int> pos; vector<int> preorder, inorder; TreeNode* build(int a,int b,int x,int y){ if(a>b) return NULL; auto root=new TreeNode(preorder[a]); int k=pos[root->val]; root->left=build(a+1, a+1+k-1-x ,x, k-1); root->right=build(a+1+k-1-x+1, b, k+1, y); return root; } TreeNode* buildTree(vector<int>& _preorder, vector<int>& _inorder) { preorder=_preorder, inorder=_inorder; int n=inorder.size(); for(int i=0;i<inorder.size();i++) pos[inorder[i]]=i; return build(0, n-1, 0, n-1); } };
|