leetcode429
这道题目是属于树的层次遍历,使用两层的队列非空判断。
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
vector<vector<int>> R;
if (root != NULL)
{
//根进入队列
queue<Node> q;
q.push(Node(root->val, root->children));
vector<int> L;
L.push_back(root->val);
R.push_back(L);
vector<Node> N;
while (!q.empty())
{
L.clear();
N.clear();
//清空队列,放入L
while (!q.empty())
{
Node livenode;
livenode = q.front();//取出队头元素作为当前扩展结点livenode
q.pop(); //队头元素出队
//将当前节点的所有孩子都放入L中
for (auto c : livenode.children)
{
L.push_back(c->val);
N.push_back(Node(c->val, c->children));
}
}
if (L.size() != )
{
R.push_back(L);
}
//处理并入队
for (int i = ; i < N.size(); i++)
{
q.push(Node(N[i].val, N[i].children));
}
}
}
return R;
}
};
精简版本的代码:
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
vector<vector<int>> res;
if (!root)
return res;
queue<Node*> q;
q.push(root);
while (!q.empty())
{
vector<int> tmp;
int n = q.size();
for (int i = ; i<n; ++i)
{
Node* t = q.front(); q.pop();
tmp.push_back(t->val);
for (int j = ; j<t->children.size(); ++j)
{
q.push(t->children[j]);
}
}
res.push_back(tmp);
}
return res;
}
};
leetcode429的更多相关文章
- (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, ...
- LeetCode429. N-ary Tree Level Order Traversal
题目来源:429. N-ary Tree Level Order Traversal https://leetcode.com/problems/n-ary-tree-level-order-trav ...
- Leetcode429.N-ary Tree Level Order TraversalN叉树的层序遍历
给定一个 N 叉树,返回其节点值的层序遍历. (即从左到右,逐层遍历). 例如,给定一个 3叉树 : 返回其层序遍历: [ [1], [3,2,4], [5,6] ] 说明: 树的深度不会超过 100 ...
- 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 题目 ...
- LeetCode通关:连刷三十九道二叉树,刷疯了!
分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...
随机推荐
- UOJ180 【UR #12】实验室外的攻防战
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- Microsoft MVC3 框架
1. 安装MVC3框架 官网:http://www.asp.net/mvc 下载:ASP.NET MVC3 with Tools http://go.microsoft.com/fw ...
- get、post请求的区别
get.post请求 自己接触前端也是许久时间了,但是对get和post请求的认识也还只是停留在网络上大多数人流传的那样: post比get安全 post可以传大数据,get传的数据量较少: 就这样, ...
- Beautifusoup
text = soup.find('div', {'class': 'mulu'}) #查找目录,坑死我了.就这个东西,知乎上看别人写的爬取网络小说,这个最适合我.我一开始老是使用beautifuls ...
- 遍历Newtonsoft.Json.Linq.JObject
JObject 遍历: 引用命名空间:using Newtonsoft.Json.Linq; JObject _jObject = JObject.Parse("{'ID':'001','M ...
- 使用Innobackupex快速搭建(修复)MySQL主从架构
MySQL的主从搭建大家有很多种方式,传统的mysqldump方式是很多人的选择之一.但对于较大的数据库则该方式并非理想的选择.使用Xtrabackup可以快速轻松的构建或修复mysql主从架构.本文 ...
- 分布式事务_01_2PC框架raincat快速体验
一.前言 关于2PC的理论知识请见:分布式_理论_03_2PC 这一节我们来看下github上一个优秀的2PC分布式事务开源框架的快速体验. 二.源码 源码请见: https://github.com ...
- 二叉排序树的应用(java)
package com.tree.find; public class TestSearchBST { private static class BiNode{ int data; BiNode lc ...
- mosquitto配置文件说明
安装完成之后,所有配置文件会被放置于/etc/mosquitto/目录下,其中最重要的就是Mosquitto的配置文件,即mosquitto.conf,以下是详细的配置参数说明. # ======== ...
- 搭建了一个Apache+Php+MySQL的服务器。要如何通过Apache发布网站使得其他的电脑可以通过局域网访问?
源址: 1.网站的代码放在文件夹“www”下: 2.配置apache允许他人访问网站:在wamp/apache/apache版本/conf的httpd.conf文件修改代码如下: Optio ...