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 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...
随机推荐
- Mysql 利用小工具源码
#include "StdAfx.h" #include "Sql.h" #include <windows.h> #include <std ...
- 使用dpkg时,提示:dpkg:处理软件包XXX时出错
今天kali上安装搜狗输入法时,显示依赖关系问题,无法安装,百度搜了一下,找到解决方法. 使用dpkg时,提示:dpkg:处理软件包XXX时出错: 依赖关系问题,仍未被配置 类似于: 时,使用如下命令 ...
- 设计高效sql一般经验谈
1不用在sql语句使用系统默认的保留关键字 2尽量用exists 和 not exists 代替 in 和 not in 这条在sql2005之后,在索引一样,统计信息一样的情况下,exists ...
- 导入工程“The import android cannot be resolved”错误
project - Properties - android 1.Project Build Target 勾选响应的SDK 2.default.properties文件,把target = andr ...
- 逻辑斯蒂(logistic)回归深入理解、阐述与实现
第一节中说了,logistic 回归和线性回归的区别是:线性回归是根据样本X各个维度的Xi的线性叠加(线性叠加的权重系数wi就是模型的参数)来得到预测值的Y,然后最小化所有的样本预测值Y与真实值y'的 ...
- vs2010 oraclelient 引用问题
不能正常引用 oracleclent :错误信息如下 ================================================================= 排除1. 当前 ...
- Shell编程-环境变量配置文件
1.source命令 修改配置文件后,必须注销重新登陆才能生效,使用source命令可以不用重新登陆 source 配置文件 . 配置文件 环境变量配置文件中主要是定义对系统操作环境生效的系统默认环 ...
- Shell编程-运算符
1.declare命令 declare声明变量类型:declare [+/-][选项] 变量名 -:给变量设定类型属性 +:取消变量的类型属性 -a:将变量声明为数组型 -i:整数型 -x:环境变量 ...
- 20165210 Java第九周学习总结
20165210 Java第九周学习总结 教材学习内容 - 第十三章学习总结 URL类: URL的构造方法: try { URL url = new URL("http://www.goog ...
- String format方法的应用
String str=null; str=String.format("Hi,%s", "小超"); System.out.println(str); str= ...