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. js文件 与 css文件 异步加载

    使用lazyload 异步加载css js 文件. 提升页面初始化的速度,减少卡顿时间 , 下面是 使用方法 与 lazyload.js 源码 (中文注释) 调用方法后. 会追加到 head 标签末尾 ...

  2. 扩展ACL

  3. php-浮点数计算,double类型数加减乘除必须用PHP提供的高精度计算函数

    一.前方有坑 php在使用加减乘除等运算符计算浮点数的时候,经常会出现意想不到的结果,特别是关于财务数据方面的计算,给不少工程师惹了很多的麻烦.比如今天工作终于到的一个案例: $a = 2586; $ ...

  4. posh-git

    https://github.com/dahlbyk/posh-git#step-2-import-posh-git-from-your-powershell-profile $profile.All ...

  5. HSCR | Hirschsprung‘s disease | 巨结肠 | 研究进展

    这个网站介绍得很详细:Hirschsprung Disease,基本的定义.病因.诊断. Hirschsprung disease — integrating basic science and cl ...

  6. Enhancer | 增强子 专题

    要做就做深做精! Everything needs good justification. The interpretation should be biologically and statisti ...

  7. gitlab 构建常见错误

    1.前端是http服务后端是https,原因生产https,测试是http服务环境.代理后端2. java 打包程序需要运行正式数据库没连上错误.打包和跑正式的一个库.3. jenkins不能直接no ...

  8. SpringCloud-Eureka配置instanceId显示IP

    eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ instance: preferIpAddress: tr ...

  9. AndoridSQLite数据库开发基础教程(4)

    AndoridSQLite数据库开发基础教程(4) 安装SQLiteManager 以下是SQLiteManager的安装步骤: (1)双击下载的.exe文件,弹出SQLiteManager Setu ...

  10. CSS清除浮动方法集合

    CSS清除浮动方法集合 一.浮动产生原因   -   TOP 一般浮动是什么情况呢?一般是一个盒子里使用了CSS float浮动属性,导致父级对象盒子不能被撑开,这样CSS float浮动就产生了. ...