[LC] 429. N-ary Tree Level Order Traversal
Given an n-ary tree, return the level order traversal of its nodes' values.
Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).
Example 1
Input: root = [1,null,3,2,4,null,5,6]
Output: [[1],[3,2,4],[5,6]]
Example 2
Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
Output: [[1],[2,3,4,5],[6,7,8,9,10],[11,12,13],[14]]
/*
// Definition for a Node.
class Node {
public int val;
public List<Node> children; public Node() {} public Node(int _val) {
val = _val;
} public Node(int _val, List<Node> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public List<List<Integer>> levelOrder(Node root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) {
return res;
}
Queue<Node> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
List<Integer> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
Node cur = queue.poll();
list.add(cur.val);
for (Node node: cur.children) {
if (node != null) {
queue.offer(node);
}
}
}
res.add(list);
}
return res;
}
}
[LC] 429. N-ary Tree Level Order Traversal的更多相关文章
- 【leetcode】429. N-ary Tree Level Order Traversal
problem 429. N-ary Tree Level Order Traversal solution1:Iteration /* // Definition for a Node. class ...
- Leetcode之广度优先搜索(BFS)专题-详解429. N叉树的层序遍历(N-ary Tree Level Order Traversal)
Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree Level Order Traversal) 给定一个 N 叉树,返回其节点值的层序遍历. (即从左到右 ...
- LeetCode 429. N叉树的层序遍历(N-ary Tree Level Order Traversal)
429. N叉树的层序遍历 429. N-ary Tree Level Order Traversal LeetCode429. N-ary Tree Level Order Traversal 题目 ...
- 429. N-ary Tree Level Order Traversal - LeetCode
Question 429. N-ary Tree Level Order Traversal Solution 题目大意: N叉树,返回每层的值,从上到下,从左到右 思路: 利用队列遍历这个N叉树 J ...
- LeetCode429. N-ary Tree Level Order Traversal
题目来源:429. N-ary Tree Level Order Traversal https://leetcode.com/problems/n-ary-tree-level-order-trav ...
- LeetCode 102. 二叉树的层次遍历(Binary Tree Level Order Traversal) 8
102. 二叉树的层次遍历 102. Binary Tree Level Order Traversal 题目描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 每 ...
- [LeetCode] Binary Tree Level Order Traversal II 二叉树层序遍历之二
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【leetcode】Binary Tree Level Order Traversal I & II
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树
Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...
随机推荐
- Vue-router(1)之component标签
1. 使用 <component>标签实现组件切换 <component> 是Vue提供的标签语法:有一个is属性,is的作用就是显示指定的组件 <template> ...
- UVA 12663 第九届省赛 高桥与低桥 线段树
题意很简单,n个桥的高度是事先给出来的,然后有m次涨水与落水的高度,问有多少座桥在这m次涨落之后 被淹超过了k次,如果某桥本身被水淹了,此时再涨水,就不能算多淹一次 看下数据10的五次方,10的五次方 ...
- macOS下的播放器
很早前用 MplayerX, 现在不能用了, 找到一个替代品 https://iina.io/. 挺不错.
- scrapy练习1
1.建立项目: #建立名为tuto的项目 scrapy startproject tuto 2.进入项目目录: cd tuto 3.建立域名任务: #minyan任务名:后面是任务对应的域名 scra ...
- MySQL学习笔记——〇六SQLAlchemy框架
我们在前面所用的方法都是在可视化的视图软件或者terminal里直接写SQL语句来对数据库进行访问,这里我们大概讲一下一个新的框架——SQLAlchemy. OEM框架 OEM的概念 对象-关系映射( ...
- opencv进行视频播放每帧处理,读取视频失败
cv::VideoCapture capture(filename); if (!capture.isOpened()) { cout << "open video error& ...
- Dynamics CRM - 不同类型字段在 Plugin 里的赋值方式
在编写 Plugin 代码之前,我们可以需要用 SDK bin 目录下的 CrmSvcUtil.exe 来将 CRM Site 上所有的 Entity 转换成类,而 Entity 里的 Field 也 ...
- h5-伪元素-before和after
做一个门票或者邮票:效果图 1.html就是两个div 2.具体css代码 <style> /*左侧长方体基本样式*/ div:nth-of-type(1){ width: 300px; ...
- order by rand()优化
优化前: SELECT id, loan_id, NAME, company FROM tablename WHERE time BETWEEN 1522512000 AND 1525103999 A ...
- h3c 瘦ap无法上线解决办法(WA4320i-ACN)
瘦ap无法上线的原因主要有两个:1.无法获取IP地址 2 .版本 胖ap转瘦ap: 1.使用网线+web对ap进行管理,默认IP地址为:192.168.0.50,用户名:admin 密码:h3capa ...