-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomework02_Validate_Binary_Search_Tree.cpp
More file actions
51 lines (36 loc) · 1.18 KB
/
Homework02_Validate_Binary_Search_Tree.cpp
File metadata and controls
51 lines (36 loc) · 1.18 KB
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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool ans = true;
long long val[2];
bool isValidBST(TreeNode* root) {
val[0] = 0;
val[1] = INT_MIN;
research_BSF(root);
return ans;
}
void research_BSF(TreeNode * node){
if(node == nullptr ) return;
//进行中序遍历,发现非上升
// val[1] = min(val[0],node->val);
// val[0] = max(val[1],node->val);
// if(val[0] >= node->val) ans = false;
// if(val[1] <= node->val) ans = false;
research_BSF(node->left);
val[0] = val[1] ;
val[1] = (long long)node->val + 1;
if(val[1] <= val[0]) ans = false;
//cout << node->val;
research_BSF(node->right);
}
};