-
Notifications
You must be signed in to change notification settings - Fork 0
/
#106 Construct Binary Tree from Inorder and Postorder Traversal.cpp
65 lines (62 loc) · 2.49 KB
/
#106 Construct Binary Tree from Inorder and Postorder Traversal.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* 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* buildTree(vector<int>& inorder, vector<int>& postorder) {
return buildTree(postorder.rbegin(), inorder.begin(), inorder.end());
}
private:
TreeNode* buildTree(vector<int>::reverse_iterator curr, vector<int>::const_iterator b, vector<int>::const_iterator e) {
TreeNode* root = new TreeNode(*curr);
auto inorderRoot = find(b, e, *curr);
auto newMax = curr;
root->right = buildRightSubtree(curr, next(inorderRoot), e, newMax);
root->left = buildLeftSubtree(curr, b, inorderRoot, newMax);
return root;
}
TreeNode* buildLeftSubtree(vector<int>::reverse_iterator parent, vector<int>::const_iterator b, vector<int>::const_iterator e, vector<int>::reverse_iterator& maximal) {
if(e - b == 0) {
return nullptr;
}
else if(e - b == 1) {
maximal = next(maximal);
return new TreeNode(*maximal);
}
else {
auto node = new TreeNode(*next(maximal));
auto inorderNode = find(b, e, node->val);
auto newMax = next(maximal);
node->right = buildRightSubtree(next(maximal), next(inorderNode), e, newMax);
node->left = buildLeftSubtree(next(maximal), b, inorderNode, newMax);
maximal = max(maximal, newMax);
return node;
}
}
TreeNode* buildRightSubtree(vector<int>::reverse_iterator parent, vector<int>::const_iterator b, vector<int>::const_iterator e, vector<int>::reverse_iterator& maximal) {
if(e - b == 0) {
return nullptr;
}
else if(e - b == 1) {
maximal = max(maximal, next(parent));
return new TreeNode(*next(parent));
}
else {
auto node = new TreeNode(*next(parent));
auto inorderNode = find(b, e, node->val);
auto newMax = next(parent);
node->right = buildRightSubtree(next(parent), next(inorderNode), e, newMax);
node->left = buildLeftSubtree(next(parent), b, inorderNode, newMax);
maximal = max(maximal, newMax);
return node;
}
}
};