889. Construct Binary Tree from Preorder and Postorder Traversal

problem

solution

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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode * build(vector<int>& preorder, int l, int r, vector<int>& postorder, int lo, int hi){
if(l>r || lo>hi) return nullptr;
TreeNode* root = new TreeNode(preorder[l]);
if(lo == hi )return root;
int val = preorder[l+1], idx = -1;
for(int i=lo;i<=hi;++i){
if(val == postorder[i]){
idx = i;
break;
}
}
root->left = build(preorder, l+1, l+1+idx-lo, postorder, lo, idx);
root->right = build(preorder, l+1+idx-lo+1,r, postorder, idx+1, hi-1);
return root;

}
TreeNode* constructFromPrePost(vector<int>& preorder, vector<int>& postorder) {
return build(preorder, 0, preorder.size()-1, postorder, 0, postorder.size()-1);
}
};

analysis

  • time complexity O(n^2)
  • space complexity O(n^2) n is node number