JavaScript DFS solution (need some review)

  • 0

    Other solutions seem to be passing the current node value downward, while mine is concatenating it upward. Is there a difference between the TC? Also, some people said the key to the question is to understand the Big-O. Would this algo be O (n^2)? With each node being n and each concat being another n? And does string interpolation add another n to the Big-O?

    var binaryTreePaths = function(node) {
        if (node === null) return [];
        let left = binaryTreePaths(node.left);
        let right = binaryTreePaths(node.right);
        let output = [];
        if (left.length === 0 && right.length === 0) {
        } else {
            output = output.concat( => {return `${node.val}->${path}`}));
            output = output.concat( => {return `${node.val}->${path}`}));
        return output;

Log in to reply

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