每日一题 --- 590. N 叉树的后序遍历[力扣][Go]

简介: 每日一题 --- 590. N 叉树的后序遍历[力扣][Go]

题目:

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

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

解题代码:

func postorder(root *Node) []int {
  var ans []int
  var def func(*Node)
  def = func(node *Node) {
    if node == nil {
      return
    }
    for _, child := range node.Children {
      def(child)
      ans = append(ans, child.Val)
    }
  }
  if root == nil {
    return nil
  }
  def(root)
  ans = append(ans, root.Val)
  return ans
}

这一题与前两天的正序遍历相同,解题思路也相同,都是使用深度搜索,当然广度搜索也行


相关文章
|
4月前
|
Go
go语言中遍历映射(map)
go语言中遍历映射(map)
127 8
|
4月前
|
存储 Go 索引
go语言中遍历字符串
go语言中遍历字符串
88 5
|
7月前
|
Python
【Leetcode刷题Python】剑指 Offer 26. 树的子结构
这篇文章提供了解决LeetCode上"剑指Offer 26. 树的子结构"问题的Python代码实现和解析,判断一棵树B是否是另一棵树A的子结构。
82 4
|
3月前
|
Go
go语言for遍历映射(map)
go语言for遍历映射(map)
99 12
|
3月前
|
Go 索引
go语言使用索引遍历
go语言使用索引遍历
54 9
|
3月前
|
Go
go语言for 遍历字符串
go语言for 遍历字符串
46 8
|
3月前
|
存储 Go 索引
go语言使用for循环遍历
go语言使用for循环遍历
81 7
|
4月前
|
Go
go语言中遍历映射同时遍历键和值
go语言中遍历映射同时遍历键和值
37 7
|
4月前
|
Go 索引
go语言遍历字符串
go语言遍历字符串
66 3
|
4月前
|
测试技术 Go 索引
go语言使用 range 关键字遍历
go语言使用 range 关键字遍历
55 3