Skip to content

Commit 6a4678a

Browse files
authored
Create Validate Binary Search Tree.java
1 parent ee3d660 commit 6a4678a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//98. Validate Binary Search Tree
2+
class Solution {
3+
public boolean isValidBST(TreeNode root) {
4+
return isValidBST(root, null, null);
5+
}
6+
7+
private boolean isValidBST(TreeNode root, TreeNode minNode, TreeNode maxNode) {
8+
if (root == null)
9+
return true;
10+
if (minNode != null && root.val <= minNode.val)
11+
return false;
12+
if (maxNode != null && root.val >= maxNode.val)
13+
return false;
14+
15+
return isValidBST(root.left, minNode, root) &&
16+
isValidBST(root.right, root, maxNode);
17+
}
18+
}

0 commit comments

Comments
 (0)