【leetcode】429. N-ary Tree Level Order Traversal
problem
429. N-ary Tree Level Order Traversal
solution1:Iteration
/*
// 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==NULL) return vector<vector<int>>();//
vector<vector<int>> res;
queue<Node*> q;
q.push(root);
while(!q.empty())
{
int size = q.size();
vector<int> curLevel;
for(int i=; i<size; ++i)
{
Node* tmp = q.front();
q.pop();
curLevel.push_back(tmp->val);
for(auto a:tmp->children)
{
q.push(a);
}
}
res.push_back(curLevel);
}
return res; }
};
参考
1. Leetcode_429. N-ary Tree Level Order Traversal;
完
【leetcode】429. N-ary Tree Level Order Traversal的更多相关文章
- 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 l ...
- LeetCode之“树”:Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal 题目链接 题目要求: Given a binary tree, return the level order traversal o ...
- LeetCode(26)-Binary Tree Level Order Traversal II
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- [LeetCode&Python] Problem 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(102) Binary Tree Level Order Traversal
题目 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to rig ...
- 【leetcode】1028. Recover a Tree From Preorder Traversal
题目如下: We run a preorder depth first search on the root of a binary tree. At each node in this traver ...
- 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- 【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 ...
- 【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, ...
随机推荐
- Python 类的式列化过程解剖
类的是劣化过程解剖 类的语法 class Dog(object): print("hello,I am a dog!") d = Dog() #实例化这个类 #此时的d就是类Dog ...
- yum downloadonly模式,保存所需软件及其依赖包
command: sudo yum install --downloadonly --downloaddir='yourderictory'
- [BOZJ2721]樱花
题目求\(\frac{1}{x}+\frac{1}{y}=\frac{1}{n!}\)已知n, x和y的正整数解的个数 设z=\(n!\) \(\frac{1}{x}+\frac{1}{y}=\fra ...
- IOS MVC与MVVM的区别
MVVM是对胖模型进行的拆分,其本质是给控制器减负,就是把一些弱业务放到VM中去处理. MVC是一切设计的基础,所有新的设计模式都是基于MVC的改进.
- gym 101081 E. Polish Fortress 几何
E. Polish Fortress time limit per test 2.0 s memory limit per test 256 MB input standard input outpu ...
- 洛谷 P2661信息传递
图论——>并查集 P2661 本蒟蒻啥也不会 这题还WA了1次 = = 最后看题解A 哭了 看题是多个(N<=200000)东西之间的关系维护 果断想到并查集 在第i个位置的a[i] 表 ...
- Android代码安全工具集
前言 原计划出一系列APP测试文章,从基础发,整个思路还在整理,秉着吹牛的态度,整理了一部分安卓代码安全的工具推荐给大家玩玩,提升一下逼格. 在这之前给大家讲讲阿旺对安全测试的理解,不管别人怎么扯,一 ...
- boost高质量随机数库 zhuan
shared_ptr<int> tmp2(new int(10)) ; int * test=tmp2.get(); std::cout<<*test<<" ...
- day17_python_1124
01 昨日内容回顾 包: 1,在内存中创建一个以包命名的空间. 2,执行包的__init__文件将文件中的名字加载到包的名称空间. 3,通过包名.名字(变量,函数,类名)方式调用这些内容. aaa.x ...
- Android 音视频深入 十五 FFmpeg 推流mp4文件(附源码下载)
源码地址https://github.com/979451341/Rtmp 1.配置RTMP服务器 这个我不多说贴两个博客分别是在mac和windows环境上的,大家跟着弄 MAC搭建RTMP服务器h ...