-
Notifications
You must be signed in to change notification settings - Fork 118
/
106. Construct Binary Tree from Inorder and Postorder Traversal.cpp
86 lines (69 loc) · 3.17 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//Runtime: 56 ms, faster than 23.77% of C++ online submissions for Construct Binary Tree from Inorder and Postorder Traversal.
//Memory Usage: 24.2 MB, less than 29.37% of C++ online submissions for Construct Binary Tree from Inorder and Postorder Traversal.
/**
* 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, int is = -1, int ie = -1, int ps = -1, int pe = -1) {
if(is > ie || ps > pe) return nullptr;
int n = inorder.size();
if(n == 0) return nullptr;
if(is == -1){
is = ps= 0;
ie = pe = n-1;
}
TreeNode* node = new TreeNode(postorder[pe]);
int iroot = find(inorder.begin(), inorder.end(), postorder[pe]) - inorder.begin();
// cout << "inorder's root at: " << iroot << endl;
int leftSize = iroot-is;
node->left = buildTree(inorder, postorder, is, iroot-1, ps, ps+leftSize-1);
node->right = buildTree(inorder, postorder, iroot+1, ie, ps+leftSize, pe-1);
return node;
}
};
//Use HashMap so we don't need to "find"
//https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/discuss/34782/My-recursive-Java-code-with-O(n)-time-and-O(n)-space
//Runtime: 24 ms, faster than 74.28% of C++ online submissions for Construct Binary Tree from Inorder and Postorder Traversal.
//Memory Usage: 24 MB, less than 36.01% of C++ online submissions for Construct Binary Tree from Inorder and Postorder Traversal.
/**
* 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:
unordered_map<int, int> val2idx;
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder, int is, int ie, int ps, int pe) {
if(is > ie || ps > pe) return nullptr;
TreeNode* node = new TreeNode(postorder[pe]);
int iroot = val2idx[postorder[pe]];
// cout << "inorder's root at: " << iroot << endl;
int leftSize = iroot-is;
node->left = buildTree(inorder, postorder, is, iroot-1, ps, ps+leftSize-1);
node->right = buildTree(inorder, postorder, iroot+1, ie, ps+leftSize, pe-1);
return node;
};
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
int n = inorder.size();
if(n == 0) return nullptr;
for(int i = 0; i < n; ++i){
val2idx[inorder[i]] = i;
}
return buildTree(inorder, postorder, 0, n-1, 0, n-1);
}
};