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

For example, given a 3-ary tree:

We should return its level order traversal:

[
[1],
[3,2,4],
[5,6]
]

Note:

  1. The depth of the tree is at most 1000.
  2. The total number of nodes is at most 5000.

-----------------------------------------------------------------------------------------------------------------------------------

这个层序遍历,自然用BFS写。和二叉树的层序遍历类似,连代码不会相差很大。

C++代码:

/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children; Node() {} Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
if(!root) return {};
queue<Node*> q;
q.push(root);
vector<vector<int> > vec;
while(!q.empty()){
vector<int> vec1;
int ans = q.size();
for(int i = ans;i > ; i--){
auto t = q.front();
q.pop();
vec1.push_back(t->val);
for(Node *cur:t->children){
q.push(cur);
}
}
vec.push_back(vec1);
}
return vec;
}
};

(N叉树 BFS) leetcode429. N-ary Tree Level Order Traversal的更多相关文章

  1. 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 题目 ...

  2. (二叉树 BFS) leetcode 107. Binary Tree Level Order Traversal II

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

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

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

  4. [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, ...

  5. LeetCode429. N-ary Tree Level Order Traversal

    题目来源:429. N-ary Tree Level Order Traversal https://leetcode.com/problems/n-ary-tree-level-order-trav ...

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

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

  7. 【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, ...

  8. 102. Binary Tree Level Order Traversal 广度优先遍历

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

  9. [LintCode] Binary Tree Level Order Traversal(二叉树的层次遍历)

    描述 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分层遍历结果: [ [3] ...

  10. 【Leetcode】【Easy】Binary Tree Level Order Traversal II

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

随机推荐

  1. 【转】Python之道

    作者:Vamei 出处:http://www.cnblogs.com/vamei Python有一个彩蛋,用下面语句调出: import this 该彩蛋的文档记录于PEP 20. 语句执行之后,终端 ...

  2. winserver-记录共享文件夹操作日志

    abstract 1.在共享文件夹上开启审计. 2.在日志中查看操作记录. 开启审计 共享文件夹属性 选择审计 添加审计用户 选择用户及审计事件 日志查看 运行eventvwr 在windowslog ...

  3. Python Learning: 02

    OK, let's continue. Conditional Judgments and Loop if if-else if-elif-else while for break continue ...

  4. Win7系统下,docker构建nginx+php7环境实践

    前面两章介绍的是Windows系统下如何安装和配置docker,主要原因在于,公司大多人数用的是Windows环境,想通过在Windows环境上,通过docker,构建一个公用的配置. 首先要说明的是 ...

  5. Linux(CentOS7)下如何配置多个JDK环境变量

    一.Linux版本 二.复制粘贴多个JDK出来,如下 cp -R jdk1.7.0_80/ jdk1.7.0_80-2 cp -R jdk1.7.0_80/ jdk1.7.0_80-3 三.配置多个J ...

  6. mvc设计模式的优点

    软件设计的理念是:高内聚,低耦合.采用三层: UI:(jsp,servlet), service:(具体的业务实现), dao:(对数据库的操作) 的设计模式来指导项目开发可以使得项目各层之间是一个粗 ...

  7. svn + nginx unit + python3自动化发布web服务方法

    本周将python web服务管理更换成nginx unit以后发现接口性能有了明显的提升,访问速度快了不少.不过有个很大的问题就是使用svn自动化发布以后,服务并没有刷新使用新的代码运行,而又不懂得 ...

  8. RubyGems系列之RubyGems初识

    转载请标明来源:https://www.cnblogs.com/zhanggui/p/9719291.html 一. 基础理解 RubyGems简称gems,它是一个用于对Ruby组件进行打包的Rub ...

  9. socket.io 出现的WebSocket is closed before the connection is established

    WebSocket is closed before the connection is established 最近socket.io是挺流行的,幼麟棋牌和一些好的开源项目也使用这个框架,在搭建其平 ...

  10. jmeter在linux服务器的安装和运行

    一.工具准备 1.下载安装xshell连接服务器工具 2.下载安装Xftp工具,向服务器传输文件工具 3.下载jdk 1.8版本:jdk-8u11-linux-x64.tar.gz 下载地址: htt ...