Javascript solution not working


  • 0
    G
    var hasPathSum = function(root, sum) {
    if (!root) return false;
    
    if (root.val - sum === 0) {
        return true;
    } else {
        return (root.left && hasPathSum(root.left, sum - root.left.val)) || (root.right && hasPathSum(root.right, sum - root.right.val));
    }
    

    };

    Hi,

    Could someone explains why my code does not return false with this input:

    	{1}, 0

  • 0
    S

    root.left is null, which is 'falsy', and the whole expression

    null && false || null && false
    

    returns null.


Log in to reply
 

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