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
}
相关推荐
卜锦元2 分钟前
EchoChat搭建自己的音视频会议系统01-准备工作
c++·golang·uni-app·node.js·音视频
sin_hielo6 分钟前
leetcode 2975
数据结构·算法·leetcode
java修仙传10 分钟前
力扣hot100:跳跃游戏
算法·leetcode·游戏
王老师青少年编程14 分钟前
2024年12月GESP真题及题解(C++七级): 燃烧
c++·题解·真题·gesp·csp·七级·燃烧
平生不喜凡桃李18 分钟前
LeetCode:LRU and LFU
算法·leetcode·哈希算法
钟离墨笺1 小时前
Go语言-->interfance{}赋值的陷阱
开发语言·后端·golang
AlenTech1 小时前
238. 除了自身以外数组的乘积 - 力扣(LeetCode)
算法·leetcode·职场和发展
老鼠只爱大米2 小时前
LeetCode经典算法面试题 #41:缺失的第一个正数(位置交换法、标记法等多种方法详解)
算法·leetcode·原地哈希·缺失的第一个正数·算法面试·位图法·集合哈希法
小镇学者2 小时前
【python】python有必要像go或者nodejs那样做多版本切换吗?
开发语言·python·golang
百度搜不到…2 小时前
背包问题递推公式中的dp[j-nums[j]]到底怎么理解
算法·leetcode·动态规划·背包问题