Java 1ms 3-liner


  • 2
    public class Solution {
        public boolean isValidBST(TreeNode root) {
            return isValidBST(root, (long) Integer.MAX_VALUE + 1, (long) Integer.MIN_VALUE - 1);
        }
        
        private boolean isValidBST(TreeNode node, long ceiling, long floor) {
            if (node == null) return true;
            if (node.val >= ceiling || node.val <= floor) return false;
            return (isValidBST(node.left, Math.min(ceiling, node.val), floor) && isValidBST(node.right, ceiling, Math.max(floor, node.val)));
        }
    }
    

Log in to reply
 

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