What's wrong with my code?


  • 0
    L
    {1,2},3
    output :false
    expected:true
    //
    
    
    
    public class Solution {
        
       private static int sumForNow = 0;
       
      public Solution(){
           sumForNow = 0;
    }
    	
    	 public boolean hasPathSum(TreeNode root, int sum) {
    		 if(root == null){
    			 return false;
    		 }
    		 
    		 sumForNow += root.val;
    		 
    		 if(sumForNow == sum && root.left == null && root.right == null){
    			 return true;
    		 }
    		 
    		 if(hasPathSum(root.left, sum)){
    			 return true;
    		 }
    		 
    		 if(hasPathSum(root.right, sum)){
    			 return true;
    		 }
    		 
    		 sumForNow -= root.val;
    		 return false;
    		 
    	    }
    }

  • 0
    0

    Hi buddy , what's your solution to this problem? I have coded in way similar to yours, and happens to get the same error with you. Driven me mad, Need your help my friend. A thousand thanks!


  • 0
    0

    According to my passed version, the problem may simply be it that the judging module doesn't support solutions using static values.


  • 0
    0

    This is weird cause I have refer to static variables before in other problem sets and get accepted.


Log in to reply
 

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