Skip to content

Commit 47fe1ff

Browse files
committed
106
1 parent 1747c44 commit 47fe1ff

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

106.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
public:
3+
TreeNode* buildTree(vector<int>& postorder, vector<int>& inorder) {
4+
int ins=0;
5+
int ine=inorder.size()-1;
6+
int posts=0;
7+
int poste=postorder.size()-1;
8+
return treehelper(postorder,inorder,ins,ine,posts,poste);
9+
}
10+
TreeNode * treehelper(vector<int>in,vector<int>pos, int inS, int inE , int posS, int posE){
11+
if(inS >inE){
12+
return NULL;
13+
}
14+
int rootdata=pos[posE];
15+
int rootindex;
16+
for(int i=inS;i<=inE;i++){
17+
if(in[i]==rootdata){
18+
rootindex=i;
19+
break;
20+
}
21+
}
22+
int rootindex1;
23+
for(int j=posS;j<=posE;j++){
24+
if(pos[j]==rootdata){
25+
rootindex1=j;
26+
break;
27+
}
28+
}
29+
int LinS=inS;
30+
int LinE=rootindex-1;
31+
int LposS=posS;
32+
int LposE=LinE-LinS+LposS;
33+
int RinS=rootindex+1;
34+
int RinE=inE;
35+
int RposS=LposE+1;
36+
int RposE=rootindex1-1;
37+
TreeNode * node1= new TreeNode(rootdata);
38+
node1->left=treehelper(in,pos,LinS,LinE,LposS,LposE);
39+
node1->right=treehelper(in,pos,RinS,RinE,RposS,RposE);
40+
return node1;
41+
}
42+
};

0 commit comments

Comments
 (0)