| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 |
```TreeNode* helper(vector<int>pre,int startPre,int endPre,vector<int>in,int startIn,int endIn)
{
if(startPre>endPre||startIn>endIn)
return nullptr;
TreeNode * root=new TreeNode(pre[startPre]);
for(int i=startIn;i<=endIn;++i)
{
if(in[i]==pre[startPre])
{
root->left=helper(pre,startPre+1,startPre+i-startIn,in,startIn,i-1);
root->right=helper(pre,i-startIn+startPre+1,endPre,in,i+1,endIn);
break;
}
}
return root;
}
TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin)
{
TreeNode *root=helper(pre,0,pre.size()-1,vin,0,()-1);
return root;
}
|