confusion in JS array [leetcode 257]
Anonymous User
100

why the currArr = [... currArr, node.val] is giving me correct output?

isnt it should behave same as currArr.push(node.val)

var binaryTreePaths = function(root) {
    
    let res = [];
    const currArr = [];
    
    function traverse(node, currArr){
        
        if(!node) return;
        
        currArr.push(node.val);                <------   [[1,2,5,3],[1,2,5,3]]
        // currArr = [...currArr, node.val]    <------   [[1,2,5],[1,3]]
        
        if(!node.left && !node.right){         
            res.push(currArr)       
            return;                           
        }

        
        
        traverse(node.left, currArr)

        traverse(node.right, currArr)

        
    }
    
    traverse(root, currArr);
    return res;
    
};
Comments (0)