(N叉树 BFS) leetcode429. 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).
For example, given a 3-ary tree:

We should return its level order traversal:
[
[1],
[3,2,4],
[5,6]
]
Note:
- The depth of the tree is at most
1000. - 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的更多相关文章
- 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 题目 ...
- (二叉树 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 ...
- Leetcode之广度优先搜索(BFS)专题-详解429. N叉树的层序遍历(N-ary Tree Level Order Traversal)
Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree Level Order Traversal) 给定一个 N 叉树,返回其节点值的层序遍历. (即从左到右 ...
- [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, ...
- 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] 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, ...
- 102. Binary Tree Level Order Traversal 广度优先遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- [LintCode] Binary Tree Level Order Traversal(二叉树的层次遍历)
描述 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分层遍历结果: [ [3] ...
- 【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 ...
随机推荐
- 【转】Python之道
作者:Vamei 出处:http://www.cnblogs.com/vamei Python有一个彩蛋,用下面语句调出: import this 该彩蛋的文档记录于PEP 20. 语句执行之后,终端 ...
- winserver-记录共享文件夹操作日志
abstract 1.在共享文件夹上开启审计. 2.在日志中查看操作记录. 开启审计 共享文件夹属性 选择审计 添加审计用户 选择用户及审计事件 日志查看 运行eventvwr 在windowslog ...
- Python Learning: 02
OK, let's continue. Conditional Judgments and Loop if if-else if-elif-else while for break continue ...
- Win7系统下,docker构建nginx+php7环境实践
前面两章介绍的是Windows系统下如何安装和配置docker,主要原因在于,公司大多人数用的是Windows环境,想通过在Windows环境上,通过docker,构建一个公用的配置. 首先要说明的是 ...
- 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 ...
- mvc设计模式的优点
软件设计的理念是:高内聚,低耦合.采用三层: UI:(jsp,servlet), service:(具体的业务实现), dao:(对数据库的操作) 的设计模式来指导项目开发可以使得项目各层之间是一个粗 ...
- svn + nginx unit + python3自动化发布web服务方法
本周将python web服务管理更换成nginx unit以后发现接口性能有了明显的提升,访问速度快了不少.不过有个很大的问题就是使用svn自动化发布以后,服务并没有刷新使用新的代码运行,而又不懂得 ...
- RubyGems系列之RubyGems初识
转载请标明来源:https://www.cnblogs.com/zhanggui/p/9719291.html 一. 基础理解 RubyGems简称gems,它是一个用于对Ruby组件进行打包的Rub ...
- socket.io 出现的WebSocket is closed before the connection is established
WebSocket is closed before the connection is established 最近socket.io是挺流行的,幼麟棋牌和一些好的开源项目也使用这个框架,在搭建其平 ...
- jmeter在linux服务器的安装和运行
一.工具准备 1.下载安装xshell连接服务器工具 2.下载安装Xftp工具,向服务器传输文件工具 3.下载jdk 1.8版本:jdk-8u11-linux-x64.tar.gz 下载地址: htt ...