javascript-leetcode icon indicating copy to clipboard operation
javascript-leetcode copied to clipboard

590. N 叉树的后序遍历

Open Geekhyt opened this issue 4 years ago • 0 comments

原题链接

递归 dfs

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

Geekhyt avatar Sep 20 '21 13:09 Geekhyt