Typical INT_MIN, INT_MAX solution and getting around leetcode testcase against it ;)


  • 0
    R

    This is a solution only if you still want to use Integer.MIN_VAL Integer.MAX_VAL. Leetcode has added a test against solution since I think the expectation is that people do inorder traversal or bfs based solution for this problem. Please practice those solutions too.

        public boolean isValidBST(TreeNode root) {
            return isValidBST(root, null, null);
        }
    
        private boolean isValidBST(TreeNode root, Integer low, Integer high) {
            if (root == null) {
                return true;
            }
    
            if ((low != null && root.val <= low) || (high != null && root.val >= high)) {
                return false;
            }
    
            return isValidBST(root.left, low, root.val) && isValidBST(root.right, root.val, high);
        }
    

Log in to reply
 

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