Golang | Leetcode Golang题解之第429题N叉树的层序遍历

题目:

题解:

Go 复制代码
func levelOrder(root *Node) (ans [][]int) {
    if root == nil {
        return
    }
    q := []*Node{root}
    for q != nil {
        level := []int{}
        tmp := q
        q = nil
        for _, node := range tmp {
            level = append(level, node.Val)
            q = append(q, node.Children...)
        }
        ans = append(ans, level)
    }
    return
}
相关推荐
不会写DN1 天前
Golang中的map的key可以是哪些类型?可以嵌套map吗?
后端·golang·go
6Hzlia1 天前
【Hot 100 刷题计划】 LeetCode 739. 每日温度 | C++ 逆序单调栈
c++·算法·leetcode
XWalnut1 天前
LeetCode刷题 day16
数据结构·算法·leetcode·链表·动态规划
memcpy01 天前
LeetCode 2452. 距离字典两次编辑以内的单词【暴力;字典树】中等
算法·leetcode·职场和发展
止语Lab1 天前
Go vs Java GC:同一场延迟战争的两条路
java·开发语言·golang
We་ct1 天前
LeetCode 322. 零钱兑换:动态规划入门实战
前端·算法·leetcode·typescript·动态规划
6Hzlia1 天前
【Hot 100 刷题计划】 LeetCode 394. 字符串解码 | C++ 单栈回压法
c++·算法·leetcode
穿条秋裤到处跑1 天前
每日一道leetcode(2026.04.22):距离字典两次编辑以内的单词
算法·leetcode
MmeD UCIZ1 天前
GO 快速升级Go版本
开发语言·redis·golang
水木流年追梦1 天前
CodeTop Top 300 热门题目2-最长回文子串
开发语言·人工智能·python·算法·leetcode