590. N 叉树的后序遍历

给定一个 N 叉树,返回其节点值的 后序遍历 。

N 叉树 在输入中按层序遍历进行序列化表示,每组子节点由空值 null 分隔(请参见示例)。

链接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* // Definition for a Node.
* function Node(val,children) {
* this.val = val;
* this.children = children;
* };
*/

/**
* @param {Node} root
* @return {number[]}
*/
var postorder = function(root) {
let node = root
let res = []
if (root === null) return res
let nodeStack = [node]
while(nodeStack.length) {
const node = nodeStack.pop()
res.unshift(node.val)
const { children = [] } = node
nodeStack = nodeStack.concat(children)
}
return res
};