方法一:递归
/*** Definition for a Node.* type Node struct {* Val int* Children []*Node* }*/func preorder(root *Node) []int {if root == nil {return []int{}}//加入当前节点的值ret := []int{root.Val}//遍历所有子节点为根的树并加入结果中for i := 0; i < len(root.Children); i++ {ret = append(ret, preorder(root.Children[i])...)}return ret}
