-
Notifications
You must be signed in to change notification settings - Fork 0
/
p106.cpp
50 lines (41 loc) · 1.21 KB
/
p106.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
#include <bits/stdc++.h>
using namespace std;
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
{
unordered_map<int, int> map;
TreeNode *solve(vector<int> &inorder, vector<int> &postorder, int &index, int inStart, int inEnd)
{
if (index < 0 || inStart > inEnd)
return NULL;
TreeNode *root = new TreeNode(postorder[index--]);
int position = map[root->val];
root->right = solve(inorder, postorder, index, position + 1, inEnd);
root->left = solve(inorder, postorder, index, inStart, position - 1);
return root;
}
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder)
{
int index = inorder.size() - 1;
int inStart = 0;
int inEnd = inorder.size() - 1;
for (int i = 0; i < inorder.size(); i++)
{
map[inorder[i]] = i;
}
return solve(inorder, postorder, index, inStart, inEnd);
}
};
int main()
{
return 0;
}