Javascript solution


  • 0
    R
    var pathSum = function(root, sum) {
        
        var result = [];
        
        var explore = function(root, sum, arr) {
            if(root === null)
                return;
            
            sum -= root.val;
            arr.push(root.val);
            
            if(sum === 0 && root.left === null && root.right === null){
                result.push(arr);
            }else{
                explore(root.left, sum, arr.slice());
                explore(root.right, sum, arr.slice());
            }
            
        };
        
        explore(root, sum, new Array());
        
        return result;
        
    };

  • 0

    why arr.slice( )?


  • 0

    @nagisa1993 copy the array by value not by reference


Log in to reply
 

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