Why I cannot use Double.MAX_VALUE/MIN_VALUE?

  • 2

    Pretty straightforward recursive method, however, it could not pass {0, -1} test case. I modified Double.MAX_VALUE/MIN_VALUE to Long.MAX_VALUE/MIN_VALUE, it got passed. What's the difference between these two types when dealing with integers?

    public boolean isValidBST(TreeNode root) {
        if(root == null || root.left == null && root.right == null) return true;
        return isValid(root, Double.MAX_VALUE, Double.MIN_VALUE);
    public boolean isValid(TreeNode root, double maxBound, double minBound) {
        if(root == null ) return true;
        if(root.val <= minBound || root.val >= maxBound) return false;
        return isValid(root.left, root.val, minBound) && isValid(root.right, maxBound, root.val);

  • 0

    Can you send the test case for which it does not work?

  • 0

    Double.MIN_VALUE is a positive number

Log in to reply

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