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
}
相关推荐
flashlight_hi9 小时前
LeetCode 分类刷题:141. 环形链表
javascript·算法·leetcode
Kt&Rs11 小时前
11.9 LeetCode 题目汇总与解题思路
算法·leetcode
ゞ 正在缓冲99%…11 小时前
leetcode1547.切棍子的最小成本
数据结构·算法·leetcode·动态规划
2401_8414956411 小时前
【LeetCode刷题】移动零
数据结构·python·算法·leetcode·数组·双指针法·移动零
开心星人12 小时前
Leetcode hot100 Java刷题(二)
java·算法·leetcode
hn小菜鸡12 小时前
LeetCode 153.寻找旋转排序数组中的最小值
数据结构·算法·leetcode
古城小栈15 小时前
Go 1.25 发布:性能、工具与生态的全面进化
开发语言·后端·golang
Ryan ZX16 小时前
【Go语言基础】序列化和反序列化
开发语言·后端·golang
蒙奇D索大16 小时前
【算法】递归算法的深度实践:深度优先搜索(DFS)从原理到LeetCode实战
c语言·笔记·学习·算法·leetcode·深度优先
一匹电信狗16 小时前
【C++11】右值引用+移动语义+完美转发
服务器·c++·算法·leetcode·小程序·stl·visual studio