Javascript solution beats 83%


  • 0
    /**
     * @param {TreeNode} root
     * @param {number} sum
     * @return {boolean}
     */
    var hasPathSum = function(root, sum) {
        if (!root) return false;
    
        if (!(root.left || root.right)) {
          if (root.val === sum) return true;
          else return false;
        }
        let diff = sum - root.val;
        return hasPathSum(root.left, diff) || hasPathSum(root.right, diff);
    };

Log in to reply
 

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