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 ...
随机推荐
- 实现div里的img图片水平垂直居中
body结构 <body> <div> <img src="1.jpg" alt="haha"> </div> ...
- haproxy配置详解
先看一个ha的配置文件: # # Global settings # global # to have these messages end up in /var/log/haproxy.log yo ...
- ssh免密码登录配置方法
每次输密码很麻烦,免密登录设置方法按照<ssh免密码登录配置方法>即可,简单来说: 1.终端上执行ssh-keygen -t rsa,生成密钥对(存放在/home/usera/.ssh). ...
- 在Android 5.0中使用JobScheduler(转载)
翻译见:http://blog.csdn.net/bboyfeiyu/article/details/44809395 In this tutorial, you will learn how to ...
- Linux报“ '/usr/bin' is not included in the PATH environment variable”解决方法
https://www.cnblogs.com/alvinwei1024/p/4811993.html https://blog.csdn.net/drbinzhao/article/details/ ...
- SpringBoot------全局异常捕获和自定义异常
1.添加Maven依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://w ...
- 【python】关键网站
https://pypi.org https://www.python.org/search/?q=pyhanlp&submit= https://www.lfd.uci.edu/~gohlk ...
- 用 SQLite 和 FMDB 替代 Core Data
本文转载至 http://blog.csdn.net/majiakun1/article/details/38680147 为什么我不使用Core Data Mike Ash 写到: 就个人而言,我不 ...
- Centos 安装 nload
第一种方法: wget http://mirrors.kernel.org/fedora-epel/6/i386/epel-release-6-8.noarch.rpm rpm -ivh epel-r ...
- E - Radar Installation
Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...