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 30 31 32 33 34 35 36 37 38 39 40 41
|
class Solution { private: vector<int> pre; vector<int> vin; public: TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) { if(pre.size()==0 || vin.size()==0 || vin.size()!=pre.size()) return NULL; this->pre=pre; this->vin=vin; TreeNode* root; constructor(0,pre.size()-1,0,pre.size()-1,&root); return root; } void constructor(int pLeft,int pRight,int vLeft,int vRight,TreeNode** node){ if(pLeft>pRight) return; bool found=false; int vPos=vLeft; while(vPos<=vRight){ if(pre[pLeft]==vin[vPos]){ found=true; break; } vPos++; } if(!found) return; *node=new TreeNode(pre[pLeft]); if(pLeft==pRight) return; int leftLength=vPos-vLeft; constructor(pLeft+1,pLeft+leftLength,vLeft,vPos-1,&((*node)->left)); constructor(pLeft+leftLength+1,pRight,vPos+1,vRight,&((*node)->right)); } };
|