My Simple Javascript solution with O(n) Space and O(n) time


  • 0
    Y
    /**
     * Definition for a binary tree node.
     * function TreeNode(val) {
     *     this.val = val;
     *     this.left = this.right = null;
     * }
     */
    /**
     * @param {TreeNode} root
     * @param {number} sum
     * @return {number[][]}
     */
    var pathSum = function(root, sum) {
        if(root === null)
            return [];
            
        var stack = [];
        var res = [];
        stack.push([root, 0, []]);
        
        while(stack.length > 0){
            var items = stack.pop();
            var node = items[0];
            var sm = items[1];
            var array = items[2].slice();//deep copy this array
            sm = sm + node.val;
            array.push(node.val);
    
            if(!node.left && !node.right && sm === sum)
                res.push(array);
            if(node.right)
                stack.push([node.right, sm, array]);
            if(node.left)
                stack.push([node.left, sm, array]);
        }
        
        return res;
    };

  • 0
    J

    Nice! I think this should perform similarly (I'm not great calculating Big O notation?

    var pathSum = function(root, sum) {
        var results = [];
        sumPath(root, sum, [], results);
        return results
    };
    
    function sumPath(node, sum, path, results) {
        if(node === null) return;
        
        sum -= node.val;
        path.push(node.val);
        
        if(node.left === null && node.right === null && sum === 0) results.push(path);
        
        sumPath(node.left, sum, path.slice(), results);
        sumPath(node.right, sum, path.slice(), results);
    }

Log in to reply
 

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