/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) { if(vin.size()==0) { return NULL; } TreeNode *head=new TreeNode(pre[0]); vector<int> pre_r,pre_l,vin_r,vin_l; int t=0; while(vin[t]!=pre[0]) { t++; } for(int i=0;i<t;i++) { pre_l.push_back(pre[i+1]); vin_l.push_back(vin[i]); } for(int i=t+1;i<vin.size();i++) { pre_r.push_back(pre[i]); vin_r.push_back(vin[i]); } head->right=reConstructBinaryTree(pre_r,vin_r); head->left=reConstructBinaryTree(pre_l,vin_l); return head; } };