「已注销」 2023-09-07 23:51 采纳率: 50%
浏览 8

时间复杂度和空间复杂度

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {string[]}
 */
var binaryTreePaths = function (root) {
    const paths = [];
    function findAllPaths(node, path) {
        if (!node) return;
        const { val, left, right } = node;
        path.push(val);
        !left && !right && paths.push(path.join("->"));
        findAllPaths(node.left, path);
        findAllPaths(node.right, path);
        path.pop();
    }
    findAllPaths(root, []);
    return paths;
};

时间复杂度和空间复杂度是多少?

  • 写回答

2条回答 默认 最新

  • threenewbee 2023-09-07 23:55
    关注

    时间空间复杂度都是O(2^N)

    评论

报告相同问题?

问题事件

  • 创建了问题 9月7日