8 lines java Solution,recursion,easy to understand


  • 1
    D
      	public boolean hasPathSum( TreeNode root, int sum ) {
    	if( root != null ) {
    		sum -= root.val;
    	  if( sum == 0 && root.left == null && root.right == null ) return true;
    		else return hasPathSum( root.left, sum ) || hasPathSum( root.right, sum );
    	} else 
    		return false;
    }

Log in to reply
 

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