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 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...
随机推荐
- ReverseInteger
public class ReverseInteger { public static int reverse(int x) { long ret = 0; //如果是个位数,直接返回. if(x/1 ...
- release与debug的区别
http://www.cnblogs.com/JemBai/archive/2009/01/13/1374805.html
- hdu 3410 单调栈
http://acm.hdu.edu.cn/showproblem.php?pid=3410 Passing the Message Time Limit: 2000/1000 MS (Java/Ot ...
- Linux中的固件加载例子
AP6335模块(BCM4339)在上电运行时,是需要刷入固件的,其在普通WIFI模式和AP模式之间切换时,也是需要加载不同的固件的,其位于/system/etc/firmware/下面:fw_bcm ...
- Handsontable 的数据保存(增删改查+导出excel)
项目用到handsontable 插件 根据官网 API写的handsontable初始化, 数据展示, ajax请求, 参数封装, Controller参数接受 全局容器 var AllData = ...
- SpringBoot WebSocket实现
1.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- GEF入门实例_总结_03_显示菜单和工具栏
一.前言 本文承接上一节: GEF入门实例_总结_02_新建初始RCP空项目 这一节,我们来给我们的插件加上菜单. 二.基础知识 1.action bar.menubar.coolbar 含义 a ...
- hdoj-1017-A Mathematical Curiosity(格式坑)
题目链接 /* Name: Copyright: Author: Date: 2018/5/3 16:32:15 Description: */ #include <iostream> # ...
- Leetcode 969. Pancake Sorting
每次找到当前最大数,转两下把最大数转到最右边.重复这个操作,直到都转完. 时间复杂度O(n**2) class Solution(object): def pancakeSort(self, A): ...
- Codeforces Round #277.5 (Div. 2)D Unbearable Controversy of Being (暴力)
这道题我临场想到了枚举菱形的起点和终点,然后每次枚举起点指向的点,每个指向的点再枚举它指向的点看有没有能到终点的,有一条就把起点到终点的路径个数加1,最后ans+=C(路径总数,2).每两个点都这么弄 ...