-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathsolution.cpp
46 lines (44 loc) · 821 Bytes
/
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
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution
{
public:
int tmp;
bool tag;
bool key;
bool isValidBST(TreeNode *root)
{
tag = false;
key = true;
check(root);
return key;
}
void check(TreeNode *root)
{
if(key == false)
return;
if(root == NULL)
return;
check(root->left);
if(tag == false)
{
tmp = root->val;
tag = true;
}
else
{
if(root->val <= tmp)
key = false;
else
tmp = root->val;
}
check(root->right);
}
};