589. N叉树的前序遍历

image.png

  1. package main
  2. type Node struct {
  3. Val int
  4. Children []*Node
  5. }
  6. func preorder(root *Node) []int {
  7. if root==nil{
  8. return nil
  9. }
  10. res :=make([]int,0)
  11. helper(root,&res)
  12. return res
  13. }
  14. func helper(root *Node,res *[]int){
  15. if root==nil{
  16. return
  17. }
  18. *res = append(*res,root.Val)
  19. for i:=0;i<len(root.Children);i++{
  20. helper(root.Children[i],res)
  21. }
  22. }
  23. func main() {
  24. }

image.png