Testcase {1, 2} my output in eclipse is 2 but oj is 1, could anyone tell me the reason, thanks


  • 0
    L

    public class Solution {
    static int min=1000000;
    static int max=0;
    public int minDepth(TreeNode root)
    {

    	if(root==null) {return 0;}
    
    	int answer =Cal(root,1);
    	return min;
    }
    
    public int Cal(TreeNode Node,int Depth)
    {
    	boolean flag=false;
    
    	if(Node.left!=null)
    	{   
    		flag=true;
    		Depth= Cal(Node.left,++Depth); 
    	}
    
    	if(Node.right!=null)
    	{
    		flag=true;
    	    Depth= Cal(Node.right,++Depth);
    	    Depth-=1;
    	    
    			  
    	}
    	else
    	{
    		
    		if(Depth>=max){ max=Depth;   }
    		if(Depth<=min&&flag==false){ min=Depth; }
    		Depth-=1;
    			
    	}
    	return Depth;
    }
    

    }


  • 0
    G

    i came across the same question, dont know why


  • 0
    W

    I came across the same problem. Do you solve yours?


  • 0

    The reason is you are declaring two static variables min and max. Please read the FAQ for more details, specifically the question Why does my code produce a different output compared to my local environment?


  • 0
    P

    i believe test cases to test solution is written for checking Height of the binary tree.
    AFAIK depth of tree refers to no of edges between root node to leaf node.
    Now wiht tree {1,2} i dont see any reason to have 2 edges in BT.

    Please correct me if wrong.


Log in to reply
 

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