【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, ...
随机推荐
- UI自动化(三)css优先级
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- [c/c++] programming之路(12)、循环结构
一.求2n #include<stdio.h> void main(){ ; ; while(n--){ s*=; printf("%d,%d\n",s,n); } g ...
- ARM基础
ARM汇编:(APCS过程调用标准) 汇编:用助记符(如$ # .)代替操作码,用地址符号或标签代替地址码的编程语言 特点: 优点:可以直接访问硬件,目标代码简短,执行速度快(CPU启动时需要直接操作 ...
- HDU 4010 Query on The Trees(动态树)
题意 给定一棵 \(n\) 个节点的树,每个点有点权.完成 \(m\) 个操作,操作四两种,连接 \((x,y)\) :提 \(x\) 为根,并断 \(y\) 与它的父节点:增加路径 \((x,y)\ ...
- vi中换行、翻页和查找功能
vi时,按ESC进入Command模式, 1. 移动光标命令:j 向下移动一行:k 向上移动一行:h 向左移动一个字符:l 向右移动一个字符:对于 j.k.l和h键,命令前加数字,移动多行:如 3j, ...
- PHP isset 和 array_key_exists 对比
经常使用 isset 判断变量或数组中的键是否存在,但是数组中可以使用 array_key_exists 这个函数,那么这两个 哪一个更优呢? 官方文档这样定义两者: isset:语言构造器,用于检测 ...
- _quest_mod
该功能实现对任务的优化,设定接受任务的条件,比如VIP等级几或者军衔多少持有何种物品才可以接受任务,同时可以配置任务的随机奖励及几率,以上修改都会在任务文本中体现.还支持任务传送功能,接完任务后,可和 ...
- 重建索引报错-python数据分析
obj3 = pd.Series([']) obj3.reindex(range(), method='ffill') 此时会爆出一大堆错误. 出错原因是:之前 obj3 的索引是字符串类型,重新索引 ...
- 机器学习实战1-2.1 KNN改进约会网站的配对效果 datingTestSet2.txt 下载方法
今天读<机器学习实战>读到了使用k-临近算法改进约会网站的配对效果,道理我都懂,但是看到代码里面的数据样本集 datingTestSet2.txt 有点懵,这个样本集在哪里,只给了我一个文 ...
- Hive数据导入导出
Hive三种不同的数据导出的方式 (1) 导出到本地文件系统 insert overwrite local directory '/home/anjianbing/soft/export_data/ ...