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
26
27
28
| // @Title: N 叉树的后序遍历 (N-ary Tree Postorder Traversal)
// @Author: 15816537946@163.com
// @Date: 2022-03-12 10:28:50
// @Runtime: 0 ms
// @Memory: 3.9 MB
/**
* Definition for a Node.
* type Node struct {
* Val int
* Children []*Node
* }
*/
func postorder(root *Node) []int {
res := make([]int, 0)
var dfs func(*Node)
dfs = func(node *Node) {
if node == nil {
return
}
for _, v := range node.Children {
dfs(v)
}
res = append(res, node.Val)
}
dfs(root)
return res
}
|