98. Validate Binary Search Tree#
1class Solution {
2
3 public boolean isValidBST(TreeNode root) {
4 return isValidBST(root, Long.MIN_VALUE, Long.MAX_VALUE);
5 }
6
7 private boolean isValidBST(TreeNode root, long min, long max) {
8 if (root == null) {
9 return true;
10 }
11
12 if (root.val <= min || root.val >= max) {
13 return false;
14 }
15
16 return (
17 isValidBST(root.left, min, root.val) &&
18 isValidBST(root.right, root.val, max)
19 );
20 }
21}