-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP98.cpp
39 lines (36 loc) · 941 Bytes
/
P98.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
#include "header.h"
class Solution {
public:
bool isValidBST(TreeNode* root) {
if (!root) return true;
long long vval = root->val;
// basic
if (root->left && root->left->val >= vval)
return false;
if (root->right && root->right->val <= vval)
return false;
bool ok1,ok2;
if (vval >= lt || vval <= bt)
return false;
long long tmp;
tmp = lt;
lt = min(vval,lt);
ok1 = isValidBST(root->left);
lt = tmp;
if (!ok1) return false;
tmp = bt;
bt = max(vval,bt);
ok2 = isValidBST(root->right);
bt = tmp;
if (!ok2) return false;
return true;
}
long long lt=INT64_MAX,bt=INT64_MIN;
};
int main() {
auto p = new TreeNode(2);
p->left = new TreeNode(1);
p->right= new TreeNode(3);
cout << Solution().isValidBST(p);
return 0;
}