-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path105.cpp
45 lines (35 loc) · 1.33 KB
/
105.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
/**
* 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> hash;
int index = 0;
int size;
TreeNode * divide (vector <int>& preorder, int l, int r){
if (r-l < 0 || (index+1) >= size || hash[preorder[index+1]] < l || hash[preorder[index+1]] > r) return nullptr;
index++;
TreeNode * cur = new TreeNode(preorder[index]);
cur -> left = divide (preorder, l, hash[preorder[index]] - 1);
cur -> right = divide (preorder, hash[preorder[index]] + 1, r);
return cur;
}
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
size = inorder.size();
for (int i = 0; i < size; i++ ){
hash[inorder[i]] = i;
}
TreeNode * first = new TreeNode(preorder[0]);
first -> left = divide (preorder, 0, hash[preorder[0]] - 1);
first -> right = divide (preorder, hash[preorder[0]] + 1, size-1);
return first;
}
};