-
Notifications
You must be signed in to change notification settings - Fork 3
/
889.cpp
61 lines (59 loc) · 2.04 KB
/
889.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
/**
* 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* construct(vector<int>& preorder, int& idx, vector<int>& postorder, int left, int right) {
cout << idx << " " << left << " " << right << endl;
if (idx >= preorder.size()) return nullptr;
if (left > right) return nullptr;
TreeNode* node = new TreeNode(preorder[idx]);
idx++;
if (left == right) return node;
int middle = right;
while (postorder[middle] != preorder[idx]) middle--;
node->left = construct(preorder, idx, postorder, left, middle);
node->right = construct(preorder, idx, postorder, middle + 1, right - 1);
return node;
}
TreeNode* constructFromPrePost(vector<int>& preorder, vector<int>& postorder) {
int idx = 0;
return construct(preorder, idx, postorder, 0, preorder.size() - 1);
}
};
/**
* 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:
int preIdx = 0;
int postIdx = 0;
TreeNode* constructFromPrePost(vector<int>& preorder, vector<int>& postorder) {
TreeNode* node = new TreeNode(preorder[preIdx]);
preIdx++;
if (node->val != postorder[postIdx]) {
node->left = constructFromPrePost(preorder, postorder);
}
if (node->val != postorder[postIdx]) {
node->right = constructFromPrePost(preorder, postorder);
}
postIdx++;
return node;
}
};