-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidBST.cpp
62 lines (55 loc) · 1.53 KB
/
validBST.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
62
#include <iostream>
#include <memory>
#include <climits>
#include <vector>
using std::shared_ptr;
using std::make_shared;
using std::vector;
struct Node{
Node(int value){
this->value = value;
}
int value;
shared_ptr<Node> left{};
shared_ptr<Node> right{};
};
// For retval to be true, leftLimit < curr->value < rightLimit
bool isValidSubTree(shared_ptr<Node> curr, int leftLimit, int rightLimit){
if(curr == nullptr){
return true;
}
if(curr->value < leftLimit || curr->value > rightLimit){
return false;
}
return isValidSubTree(curr->left, leftLimit, curr->value) && isValidSubTree(curr->right, curr->value, rightLimit);
}
bool isValidBST(shared_ptr<Node> root){
return isValidSubTree(root, INT_MIN, INT_MAX);
}
shared_ptr<Node> makeTestTree(){
shared_ptr<Node> root{make_shared<Node>(8)};
root->left = make_shared<Node>(4);
root->right = make_shared<Node>(7);
return root;
}
shared_ptr<Node> createSubTree(vector<int>& input, int left, int right){
if(left > right)
return shared_ptr<Node>();
int mid{(left + right)/2};
shared_ptr<Node> root{make_shared<Node>(input[mid])};
root->left = createSubTree(input, left, mid-1);
root->right = createSubTree(input, mid+1, right);
return root;
}
shared_ptr<Node> sortedVector2Tree(vector<int> input){
if(input.empty()){
return shared_ptr<Node>();
}
return createSubTree(input, 0, input.size()-1);
}
int main(){
//auto root{makeTestTree()};
auto root{sortedVector2Tree({1,2,3,4,5,6,7})} ;
std::cout << isValidBST(root) <<std::endl;
return 0;
}