-
Notifications
You must be signed in to change notification settings - Fork 12
/
solution.cpp
69 lines (62 loc) · 1.79 KB
/
solution.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
63
64
65
66
67
68
69
#include <iostream>
using namespace std;
/**
* Definition for a binary tree node.
*/
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
TreeNode* insertLeft(TreeNode* root, int value) {
root->left = new TreeNode(value);
return root->left;
}
TreeNode* insertRight(TreeNode* root, int value) {
root->right = new TreeNode(value);
return root->right;
}
class Solution {
public:
bool isValid(TreeNode* root, int minVal, int maxVal, bool noMin, bool noMax) {
if (root == NULL) {
return true;
}
if (!noMin && root->val <= minVal) {
return false;
}
if (!noMax && root->val >= maxVal) {
return false;
}
bool rightValid = false;
bool leftValid = false;
rightValid = isValid(root->right, root->val, maxVal, false, noMax);
leftValid = isValid(root->left, minVal, root->val, noMin, false);
return rightValid && leftValid;
}
bool isValidBST(TreeNode* root) {
return isValid(root, 0, 0, true, true);
}
};
int main() {
auto root1 = new TreeNode(50);
Solution solver;
cout << solver.isValidBST(root1) << endl; // true
auto root2 = new TreeNode(50);
insertLeft(root2, 30);
insertLeft(root2->left, 10);
insertRight(root2, 70);
insertRight(root2->right, 80);
insertRight(root2->left, 40);
insertLeft(root2->right, 60);
cout << solver.isValidBST(root2) << endl; // true
auto root3 = new TreeNode(50);
insertLeft(root3, 30);
insertLeft(root3->left, 20);
insertRight(root3, 80);
insertRight(root3->right, 90);
insertRight(root3->left, 60);
insertLeft(root3->right, 70);
cout << solver.isValidBST(root3) << endl; // false
}