Java -- 2 Line Recursive Solution


  • 3
    B
    public class Solution {
        public boolean isValidBST(TreeNode root) {
            return validate(root, Long.MIN_VALUE, Long.MAX_VALUE);
        }
    
        private boolean validate(TreeNode root, long min, long max) {
            return root == null || !(root.val <= min || root.val >= max) && validate(root.left, min, root.val) && validate(root.right, root.val, max);
        }
    }
    

    1ms, and lovably asinine!


  • 1
    X
    private boolean validate(TreeNode root, long min, long max) {
        return root == null || 
        root.val > min && root.val < max
        && validate(root.left, min, root.val) 
        && validate(root.right, root.val, max);
    }

Log in to reply
 

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