JavaScript Beats 100%


  • 2
    G
    var pathSum = function(root, sum) {
        var left,
            right,
            i;
        
        if(root===null)return [];
        if(sum===root.val&&root.left===null&&root.right===null)return [[root.val]];
        
        left=pathSum(root.left,sum-root.val);
        right=pathSum(root.right,sum-root.val);
        
        for(i=0;i<left.length;i++){
            left[i].unshift(root.val);
        }
        for(i=0;i<right.length;i++){
            right[i].unshift(root.val);
        }
        return left.concat(right);
    };
    

  • 0
    G

    more simple:

    var pathSum = function(root, sum) {
        var res;
            
        if(root===null)return [];
        if(sum===root.val&&root.left===null&&root.right===null)return [[root.val]];
    
        res=pathSum(root.left,sum-root.val).concat(pathSum(root.right,sum-root.val));
        res.map(arr=>arr.unshift(root.val));
        return res;
    };
    

  • 0
    Z
    This post is deleted!

Log in to reply
 

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