Skip to content

Commit 6297829

Browse files
committed
validate binary search tree solution
1 parent 9c1260c commit 6297829

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
bool search(TreeNode* root, long min, long max){
4+
if(root == NULL)
5+
return true;
6+
7+
if(root->val <= min || root->val >= max)
8+
return false;
9+
10+
if(search(root->left, min, root->val) == false)
11+
return false;
12+
13+
if(search(root->right, root->val, max) == false)
14+
return false;
15+
16+
return true;
17+
}
18+
19+
bool isValidBST(TreeNode* root) {
20+
return search(root, (long)INT_MIN-1, (long)INT_MAX+1);
21+
}
22+
};

0 commit comments

Comments
 (0)