-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathS24.cpp
26 lines (24 loc) · 898 Bytes
/
S24.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
class Solution {
public:
int preidx;
unordered_map<int, int> m;
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder, int st, int en) {
if(st > en) return NULL;
int val = preorder[preidx++];
TreeNode* node = new TreeNode(val);
int pos = m[val];
node->left = buildTree(preorder, inorder, st, pos - 1);
node->right = buildTree(preorder, inorder, pos + 1, en);
return node;
}
TreeNode* bstFromPreorder(vector<int>& preorder) {
int n = preorder.size();
preidx = 0;
vector<int> inorder = preorder;
sort(inorder.begin(), inorder.end());
m.clear();
//Store positions of values as they appear in the inorder traversal using a hashmap
for(int i = 0; i < preorder.size(); i++) m[inorder[i]] = i;
return buildTree(preorder, inorder, 0, n - 1);
}
};