-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0098.cpp
73 lines (71 loc) · 1.54 KB
/
0098.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
70
71
72
73
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isValidBST(TreeNode *root) {
// return func1(root);
return func2(root);
}
// Wrong solution: [10,5,15,null,null,6,20]
bool func1(TreeNode *root) {
if (root == NULL) {
return true;
}
if (root->left != NULL) {
if (root->val <= root->left->val) {
return false;
}
if (func1(root->left) == false) {
return false;
}
}
if (root->right != NULL) {
if (root->val >= root->right->val) {
return false;
}
if (func1(root->right) == false) {
return false;
}
}
return true;
}
// Complexity: T(n) = 2T(n/2) + O(n) => O(nlogn)
bool func2(TreeNode *root) {
if (root == NULL) {
return true;
}
int left_max = 0, right_min = 0;
bool ret = true;
if (root->left) {
left_max = getLeftMax(root->left);
ret = ret && root->val > left_max;
}
if (root->right) {
right_min = getRightMin(root->right);
ret = ret && root->val < right_min;
}
if (ret == false) {
return false;
}
return func2(root->left) && func2(root->right);
}
int getLeftMax(TreeNode *root) {
while (root->right) {
root = root->right;
}
return root->val;
}
int getRightMin(TreeNode *root) {
while (root->left) {
root = root->left;
}
return root->val;
}
};