429. N叉树的层序遍历

429. N-ary Tree Level Order Traversal

LeetCode429. N-ary Tree Level Order Traversal

题目描述

给定一个 N 叉树,返回其节点值的层序遍历。 (即从左到右,逐层遍历)。

例如,给定一个 3 叉树:

返回其层序遍历:
```
[
[1],
[3,2,4],
[5,6]
]
```

说明:

1. 树的深度不会超过 1000。
2. 树的节点总数不会超过 5000。

Java 实现

import java.util.LinkedList;
import java.util.List;
import java.util.Queue; class Node {
public int val;
public List<Node> children; public Node() {
} public Node(int _val, List<Node> _children) {
val = _val;
children = _children;
}
} class Solution {
public List<List<Integer>> levelOrder(Node root) {
List<List<Integer>> result = new LinkedList<>();
Queue<Node> queue = new LinkedList<>();
if (root == null) {
return result;
}
queue.offer(root);
while (!queue.isEmpty()) {
List<Integer> list = new LinkedList<>();
int size = queue.size();
for (int i = 0; i < size; i++) {
if (queue.peek().children != null) {
for (int j = 0; j < queue.peek().children.size(); j++) {
queue.offer(queue.peek().children.get(j));
}
}
list.add(queue.poll().val);
}
result.add(list);
}
return result;
}
}

相似题目

参考资料

LeetCode 429. N叉树的层序遍历(N-ary Tree Level Order Traversal)的更多相关文章

  1. Leetcode之广度优先搜索(BFS)专题-详解429. N叉树的层序遍历(N-ary Tree Level Order Traversal)

    Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree Level Order Traversal) 给定一个 N 叉树,返回其节点值的层序遍历. (即从左到右 ...

  2. Java实现 LeetCode 429 N叉树的层序遍历

    429. N叉树的层序遍历 给定一个 N 叉树,返回其节点值的层序遍历. (即从左到右,逐层遍历). 例如,给定一个 3叉树 : 返回其层序遍历: [ [1], [3,2,4], [5,6] ] 说明 ...

  3. [Swift]LeetCode107. 二叉树的层次遍历 II | Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  4. 【遍历二叉树】04二叉树的层次遍历【Binary Tree Level Order Traversal】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的层次遍历的 ...

  5. LeetCode 102. 二叉树的层次遍历(Binary Tree Level Order Traversal) 8

    102. 二叉树的层次遍历 102. Binary Tree Level Order Traversal 题目描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 每 ...

  6. [LeetCode] N-ary Tree Level Order Traversal N叉树层序遍历

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

  7. 429. N叉树的层序遍历

    429. N叉树的层序遍历 题意 给定一个 N 叉树,返回其节点值的层序遍历. (即从左到右,逐层遍历). 解题思路 和二叉树的层次遍历的思想一样: 实现 class Solution(object) ...

  8. [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 ...

  9. [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

随机推荐

  1. 部署Django到云服务器(centos+nginx+mysql+uwsgi+python3)【操作篇(2)】

    接上篇操作篇(1):https://blog.csdn.net/jacky_zhuyuanlu/article/details/82880612 (七)创建Django项目 (1)建立文件夹,存放网站 ...

  2. mysql忘记密码恢复

    MySQL忘记密码恢复密码的实现方法 作者:mdxy-dxy 流传较广的方法,mysql中文参考手册上的,各位vps主机租用客户和服务器托管用户忘记mysql5.1管理员密码时,可以使用这种方法破解下 ...

  3. Fluent批处理之--windows下多个任务的计算 【转载】

    转载自http://jingcao830828.blog.163.com/blog/static/10320833620103633624506/ 1.同维多任务的连续计算 对于工程应用来说,计算精度 ...

  4. 14.LAMP服务 Linux Apache Mysql Php和防护机制 xinetd、tcp wapper

    一.安装LAMP服务 Linux Apache Mysql Php       要求操作系统支持 php解析 apache调用php插件解析 phpmyadmin       yum install ...

  5. 文献阅读 | The single-cell transcriptional landscape of mammalian organogenesis | 器官形成 | 单细胞转录组

    The single-cell transcriptional landscape of mammalian organogenesis 老板已经提了无数遍的文章,确实很nb,这个工作是之前我们无法想 ...

  6. jvm 命令使用调优 通过jstat、jmap对java程序进行性能调优

    转载:http://blog.csdn.net/jerry024/article/details/8507589 转载: https://blog.csdn.net/zhaozheng7758/art ...

  7. 000 装docker

    直接参考别人的文章,经过验证,没有问题,需要网络. URL: https://www.cnblogs.com/qgc1995/archive/2018/08/29/9553572.html 我是虚拟机 ...

  8. spring2.5 jdk1.8

    今天运行一个14年基于spring2.5的项目,出现下面错误 Unexpected exception parsing XML document from class path resource .. ...

  9. Docker容器(三)——容器端口映射

    (1).容器端口映射 容器的端口映射用到了-p选项,-p [物理机端口]:[容器实例端口] 让centos:httpd运行在后台 [root@youxi1 ~]# docker run -d -p 8 ...

  10. 123457123457#0#-----com.tym.YuErBaiKeTYM--前拼后广--育儿百科

    com.tym.YuErBaiKeTYM--前拼后广--育儿百科