Skip to content

Commit 6be738e

Browse files
committed
Create 98. Valid Binary Search Tree.cpp
1 parent be37287 commit 6be738e

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

98. Valid Binary Search Tree.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
bool isValidBST(TreeNode* root, long min_val, long max_val){
13+
if(!root) return true;
14+
return min_val < root->val && root->val < max_val &&
15+
isValidBST(root->left, min_val, root->val) && isValidBST(root->right, root->val, max_val);
16+
}
17+
bool isValidBST(TreeNode* root) {
18+
return isValidBST(root, LONG_MIN, LONG_MAX);
19+
}
20+
};

0 commit comments

Comments
 (0)