题目要求

Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

题目分析及思路

给定一棵N叉树,要求返回它的层次遍历结果。可以使用collections.deque来保存结点,按照每一层的长度来遍历结点。

python代码

"""

# Definition for a Node.

class Node:

def __init__(self, val, children):

self.val = val

self.children = children

"""

class Solution:

def levelOrder(self, root: 'Node') -> List[List[int]]:

order1 = []

q = collections.deque()

q.append(root)

while q:

order2 = []

size = len(q)

for _ in range(size):

node = q.popleft()

if not node:

continue

order2.append(node.val)

for child in node.children:

q.append(child)

if order2:

order1.append(order2)

return order1

LeetCode 429 N-ary Tree Level Order Traversal 解题报告的更多相关文章

  1. 【LeetCode】102. Binary Tree Level Order Traversal 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目描述 Given a bi ...

  2. LeetCode: Binary Tree Level Order Traversal 解题报告

    Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...

  3. 【LeetCode】429. N-ary Tree Level Order Traversal 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...

  4. 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  5. 【LeetCode】107. Binary Tree Level Order Traversal II (2 solutions)

    Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal ...

  6. 【LeetCode】102. Binary Tree Level Order Traversal (2 solutions)

    Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...

  7. 【一天一道LeetCode】#102. Binary Tree Level Order Traversal

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  8. 【LeetCode】 Binary Tree Zigzag Level Order Traversal 解题报告

    Binary Tree Zigzag Level Order Traversal [LeetCode] https://leetcode.com/problems/binary-tree-zigzag ...

  9. 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...

随机推荐

  1. sqlmap tamter

    支持的数据库 编号 脚本名称 作用 实现方式 all 1 apostrophemask.py 用utf8代替引号 ("1 AND '1'='1") '1 AND %EF%BC%87 ...

  2. 【iCore4 双核心板_ARM】例程二十三:LWIP_HTTP实验——网页服务器

    实验现象: 核心代码: int main(void) { system_clock.initialize(); led.initialize(); adc.initialize(); delay.in ...

  3. net use错误原因解决(精辟)(转)

      (1)"发生系统错误 1326. 登录失败: 未知的用户名或错误密码." 在远程机的"控制面板-文件夹选项-查看-简单的文件共享",去掉选取,然后再尝试连接 ...

  4. Redis系统性介绍

    虽然Redis已经很火了,相信还是有很多同学对Redis只是有所听闻或者了解并不全面,下面是一个比较系统的Redis介绍,对Redis的特性及各种数据类型及操作进行了介绍.是一个很不错的Redis入门 ...

  5. (笔记)Linux内核学习(三)之进程调度

    进程调度: 在可运行态进程之间分配有限处理器时间资源的内核子系统. 一 调度策略 1 进程类型 I/O消耗型进程:大部分时间用来提交I/O请求或是等待I/O请求,经常处于可运行状态,但运行时间短,等待 ...

  6. rqalpha探究 2 接入mod

    程序的目的是尽可能用mod扩展功能,所以接下来需要接入mod模块

  7. 一台PC双网卡,一个外网一个内网

    问题:一台PC双网卡,一个连外网一个连内网.用户主要访问外网,内网只访问有限的几个ip.因为外网很大,一般人公司内网常访问的ip是有限的几个. 现在如何做到在上外网的同时也能访问内网的系统?明明两个网 ...

  8. [IR] Time and Space Efficiencies Analysis of Full-Text Index Techniques

    文章阅读:全文索引技术时空效率分析 LIU Xiao-ZhuPENG Zhi-Yong 根据全文索引实现技术的不同,将其分为三大类: 索引技术 (倒排文件.签名文件 .后缀树与后缀数组) 压缩与索引混 ...

  9. SpringBoot(十七)-- 定时任务

    日常开发中,经常会使用定时任务来执行跑批,springboot默认已经帮助我们整合了定时任务. 参考:https://blog.csdn.net/u013845177/article/details/ ...

  10. Nginx对同一IP限速限流

    limit_conn_zone是限制同一个IP的连接数,而一旦连接建立以后,客户端会通过这连接发送多次请求,那么limit_req_zone就是对请求的频率和速度进行限制. limit_conn_zo ...