Java O(n) Solution, Without Using Long Type

  • 0
        public boolean isValidBST(TreeNode root) {
            if(root==null) return true;
            return isValid(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
        private boolean isValid(TreeNode root, long min, long max) {
            if(root==null) return true;
                if(root.left.val>=root.val) return false;
                if(!isValid(root.left, min, root.val-1)) return false;
            if(root.val<min || root.val>max) return false;
                if(root.right.val<=root.val) return false;
                if(!isValid(root.right, root.val+1, max)) return false;
            return true;

Log in to reply

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