98. Validate Binary Search Tree
use bst's rule and recursion
class Solution {
public boolean isValidBST(TreeNode root) {
return helper(root, Long.MIN_VALUE, Long.MAX_VALUE);
}
private boolean helper(TreeNode root, long lower, long upper) {
if (root == null) return true;
if (root.val <= lower) return false;
if (root.val >= upper) return false;
return helper(root.left, lower, root.val) && helper(root.right, root.val, upper);
}
}use inorder
Last updated