*Java* straightforward recursive solution (3 lines)

  • 4
    public boolean isValidBST(TreeNode root) {
        return isValidBST(root, Long.MIN_VALUE, Long.MAX_VALUE);
    private boolean isValidBST(TreeNode root, long min, long max) {
        return root==null || (root.val<max && root.val>min && isValidBST(root.left, min, root.val) && isValidBST(root.right, root.val, max));

  • 0

    What is the point of setting the min and max to Long.MIN_VALUE and Long.MAX_VALUE, respectively?

    Edit: Nevermind, figured it out

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.