I don't know why my code did not pass a specific test case


  • 2
    Y

    Hi All,

    Here is my code, I don't why it fails in this test case:

    Input: {2147483647}

    Output: false

    Expected: true

    public boolean isValidBST(TreeNode root , int min , int max) {
    		if (root == null)
    			return true;
    		return root.val > min 
    				&& root.val < max 
    				&& isValidBST(root.right , root.val , max )
    				&& isValidBST(root.left , min , root.val);
    	}
    	public boolean isValidBST(TreeNode root) {
    		return isValidBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
    	}

  • 1
    L

    your thinking is right except the boundary.
    you can modify it like this

     return isValidBST(root, (long)Integer.MIN_VALUE-1, (long)Integer.MAX_VALUE+1);
    

    however i don't think it is a perfect way ... for it extend Integer to long...


  • 0
    L
    This post is deleted!

  • 0
    L

    another way, you can change the parameter int to an Integer...
    then you can avoid the MIN_MAX_VALUE problem.


  • 0
    Y

    Thanks, but what is the problem of int ? is it the overflow or what ?


  • 0
    L

    if you set your boundary MIN and MAX, your judgment range will be (min, max) not [min,max]


Log in to reply
 

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