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
}
相关推荐
Storynone15 小时前
【Day20】LeetCode:39. 组合总和,40. 组合总和II,131. 分割回文串
python·算法·leetcode
j_xxx404_20 小时前
C++算法:前缀和与哈希表实战
数据结构·算法·leetcode
We་ct20 小时前
LeetCode 22. 括号生成:DFS回溯解法详解
前端·数据结构·算法·leetcode·typescript·深度优先·回溯
Frostnova丶1 天前
LeetCode 3296. 使山区高度为零的最少秒数
算法·leetcode
样例过了就是过了1 天前
LeetCode热题100 全排列
数据结构·c++·算法·leetcode·dfs
程序员夏末1 天前
【LeetCode | 第六篇】算法笔记
笔记·算法·leetcode
滴滴答滴答答1 天前
机考刷题之 23&24&25 LeetCode 55&213&123
算法·leetcode·职场和发展
无限进步_1 天前
【C++】只出现一次的数字 III:位运算的巧妙应用
数据结构·c++·git·算法·leetcode·github·visual studio
王的宝库1 天前
Go 语言:结构体:定义、初始化、方法、组合、Tag、对齐
开发语言·后端·学习·golang
一叶落4381 天前
LeetCode 74 | 搜索二维矩阵(C语言版题解)
c语言·数据结构·c++·算法·leetcode·矩阵·动态规划