-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1008.cpp
31 lines (30 loc) · 1.36 KB
/
1008.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
class Solution {
int currentPosition = 0;
TreeNode* constructBinaryTree(TreeNode* root , const int n , vector<int>& preorder , int leftRange , int rightRange) {
if ( currentPosition >= n || (preorder[currentPosition] <= leftRange || preorder[currentPosition] >= rightRange)) {
return root;
}
root = new TreeNode(preorder[currentPosition++]);
root -> left = constructBinaryTree(root -> left , n , preorder , leftRange , preorder[currentPosition-1] );
if ( currentPosition >= n || (preorder[currentPosition] <= leftRange || preorder[currentPosition] >= rightRange)) {
return root;
}
root -> right = constructBinaryTree(root -> right , n , preorder , preorder[currentPosition-1], rightRange );
return root;
}
public:
TreeNode* bstFromPreorder(vector<int>& preorder) {
currentPosition = 1;
TreeNode*root = new TreeNode(preorder[0]);
const int n = static_cast< int > ( preorder.size() );
if (n == 1) {
return root;
}
root -> left = constructBinaryTree(root->left, n, preorder, numeric_limits<int>::min(), preorder[0]);
if(currentPosition == n){
return root;
}
root -> right = constructBinaryTree(root->right, n, preorder, preorder[0] , numeric_limits<int>::max());
return root;
}
};