-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path106.cpp
46 lines (35 loc) · 1.16 KB
/
106.cpp
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
42
43
44
45
46
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
int n = inorder.size();
int m = postorder.size();
if (m !=n)
return NULL;
return buildTreeHelper(inorder, postorder, 0, n-1, 0, n-1);
}
TreeNode* buildTreeHelper(vector<int>& inorder, vector<int>& postorder, int is, int ie, int ps, int pe){
if(is > ie || ps > pe)
return NULL;
int rootval = postorder[pe];
int pos =0 ;
for(int i = is; i <= ie; i++){
if(inorder[i] == rootval){
pos = i;
break;
}
}
TreeNode* root = new TreeNode(rootval);
root->right = buildTreeHelper(inorder,postorder,pos+1,ie,ps+pos-is,pe-1);
root->left = buildTreeHelper(inorder,postorder,is,pos-1,ps,ps+pos-is-1);
return root;
}
};