Geekhyt/javascript-leetcode

589. N 叉树的前序遍历

Geekhyt opened this issue · 0 comments

原题链接

递归 dfs

const preorder = function(root) {
    if (root === null) return []
    const res = []
    function dfs(root) {
        if (root === null) return
        res.push(root.val)
        for (let i = 0; i < root.children.length; i++) {
            dfs(root.children[i])
        }
    }
    dfs(root)
    return res
}
  • 时间复杂度:O(n)
  • 空间复杂度:O(n)