-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDAY0022.cpp
More file actions
23 lines (23 loc) · 803 Bytes
/
DAY0022.cpp
File metadata and controls
23 lines (23 loc) · 803 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 98. Validate Binary Search Tree
/**
* 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 isValidBST(TreeNode* root) {
return root!=NULL&&validate(root,LONG_MAX,LONG_MIN);
}
bool validate(TreeNode*root,long max,long min){
if(root==NULL) return true;
if(root->val<=min||root->val>=max) return false;
return validate(root->left,root->val,min)&&validate(root->right,max,root->val);
}
};