Skip to content

Commit 319ec20

Browse files
authored
Create 98. Validate Binary Search Tree
1 parent 9cdb8bd commit 319ec20

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Tree/98. Validate Binary Search Tree

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
bool isValidBST(TreeNode* root) {
4+
5+
if(!root)return true;
6+
return isValidBSTHelper(root , -1e18 , 1e18 );
7+
}
8+
9+
10+
bool isValidBSTHelper(TreeNode* root , long long int min , long long int max )
11+
{
12+
if(!root)return true;
13+
14+
if(root->val>=max || root->val<=min)return false;
15+
16+
return isValidBSTHelper(root->left,min,root->val) and isValidBSTHelper(root->right,root->val,max);
17+
}
18+
};
19+
20+
//1e18 is double! It's just a typecast from double to long long int.

0 commit comments

Comments
 (0)