What is wrong with this solution?


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

  • 0
    A
    if (root.val < min || root.val > max) {
        return false;
    }
    

    must be <= and >= for exclude the equal situation


Log in to reply
 

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