-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstruct Binary Tree from Preorder and Postorder Traversal
46 lines (37 loc) Β· 1.56 KB
/
Construct Binary Tree from Preorder and Postorder Traversal
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
/**
* 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:
vector<int> x2pre, x2post; // Index mappings for quick lookup
TreeNode* f(vector<int>& preorder, vector<int>& postorder, int preS, int postS, int postE) {
if (postS>postE) return NULL; // Base case
TreeNode* root=new TreeNode(preorder[preS]);
if (postS==postE) return root; // Only 1 node
// Identify left child and find its range in postorder
int lRoot=preorder[preS+1]; // Left child is next in preorder
int lrPostIdx=x2post[lRoot]; // Index of left root in postorder
// Recursively build left and right subtrees
root->left=f(preorder, postorder, preS+1, postS, lrPostIdx);
root->right=f(preorder, postorder, x2pre[postorder[postE-1]], lrPostIdx+1, postE-1);
return root;
}
TreeNode* constructFromPrePost(vector<int>& preorder, vector<int>& postorder) {
const int n = preorder.size();
x2pre.assign(n+1, -1);
x2post.assign(n+1, -1);
for (int i=0; i<n; i++) {
x2pre[preorder[i]]=i; // Store preorder indices
x2post[postorder[i]]=i; // Store postorder indices
}
return f(preorder, postorder, 0, 0, n-1);
}
};