LeetCode 429 N-ary Tree Level Order Traversal 解题报告
题目要求
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 解题报告的更多相关文章
- 【LeetCode】102. Binary Tree Level Order Traversal 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目描述 Given a bi ...
- LeetCode: Binary Tree Level Order Traversal 解题报告
Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...
- 【LeetCode】429. N-ary Tree Level Order Traversal 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...
- 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- 【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 ...
- 【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 ...
- 【一天一道LeetCode】#102. Binary Tree Level Order Traversal
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- 【LeetCode】 Binary Tree Zigzag Level Order Traversal 解题报告
Binary Tree Zigzag Level Order Traversal [LeetCode] https://leetcode.com/problems/binary-tree-zigzag ...
- 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...
随机推荐
- [转]bootstrap table 动态列数
原文地址:https://my.oschina.net/u/2356355/blog/1595563 据说bootstrap table非常好用,从入门教程中了解到它的以下主要功能: 由于固定表头意味 ...
- Java知多少(21)this关键字详解
this 关键字用来表示当前对象本身,或当前类的一个实例,通过 this 可以调用本对象的所有方法和属性.例如: public class Demo{ public int x = 10; publi ...
- .Net MVC Cache 缓存技术总结
一.细说 ASP.NET Cache 及其高级用法 二..Net环境下的缓存技术介绍 (转) 三.asp.net中缓存的使用介绍一 四.HttpContext.Current.Cache 过期时间
- table给tbody设置滚动条
table结构例子: <table class="layui-table"> <thead> <tr> <th> 贷款项目 < ...
- highCharts图表入门简介
一.Highcharts简介 Highcharts:功能强大.开源.美观.图表丰富.兼容绝大多数浏览器的纯js图表库 Highcharts是一款纯javascript编写的图表库,能够很简单便捷的在W ...
- 【Oracle-PLsql】使用存储过程,利用table集合类型开发复杂业务报表
在一般的项目中,都需要开发一些报表,少则几个字段,多则几十个字段,需要关联的表可能多达十几.几十张表,如果想要使用一个SQL语句将这几十张表关联起来 查询所需要的字段,当你听到这里的时候,你的脑子可能 ...
- bootstrap-select 多选下拉框使用教程
http://silviomoreto.github.io/bootstrap-select/ 一.使用bootstrap-select组件时,先引用下列文件 最后一个文件 defaults-zh_C ...
- session一直报错Session store not set on request
Route::group(['middleware' => ['web']], function () { //});仍然报错,看了 session是使用默认file,没问题:app/stora ...
- python中getcwd()方法慎用
比如我们想要获取当前目录的路径,有人可能会用到getcwd()方法,但是会发现这个方法有时候不好用.下面介绍一下,getcwd()方法的具体细节: 1.os.getcwd():获取当前工作目录,也就是 ...
- ElasticSearch6(二)-- Java API连接es
此ElasticSearch系列基于最新版的6.2.4版本. 一.pom.xml依赖 <dependencies> <dependency> <groupId>ju ...