给定一个 N 叉树,返回其节点值的层序遍历。 (即从左到右,逐层遍历)。

例如,给定一个 3叉树 :

返回其层序遍历:

[ [1], [3,2,4], [5,6] ]

说明:

  1. 树的深度不会超过 1000。
  2. 树的节点总数不会超过 5000。
class Solution {
public:
vector<vector<int> > levelOrder(Node* root) {
vector<vector<int> > res;
if(root == NULL)
return res;
queue<Node*> q;
q.push(root);
while(!q.empty())
{
vector<int> temp;
int len = q.size();
for(int i = 0; i < len; i++)
{
Node* node = q.front();
q.pop();
temp.push_back(node ->val);
for(int j = 0; j < node ->children.size(); j++)
{
if(node ->children[j] != NULL)
q.push(node ->children[j]);
}
}
res.push_back(temp);
}
return res;
}
};

Leetcode429.N-ary Tree Level Order TraversalN叉树的层序遍历的更多相关文章

  1. [LeetCode] Binary Tree Level Order Traversal II 二叉树层序遍历之二

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  2. [LeetCode] 107. Binary Tree Level Order Traversal II 二叉树层序遍历 II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  3. leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)

    题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...

  4. Binary Tree Level Order Traversal II(层序遍历2)

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  5. 【LeetCode】102. Binary Tree Level Order Traversal 二叉树的层序遍历 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://lee ...

  6. [Leetcode] Binary tree level order traversal ii二叉树层次遍历

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  7. [LeetCode107]Binary Tree Level Order Traversal II 二叉树层次遍历

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

  8. LeetCode 102. Binary Tree Level Order Traversal 二叉树的层次遍历 C++

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  9. LeetCode 102. Binary Tree Level Order Traversal02. 二叉树的层次遍历 (C++)

    题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...

随机推荐

  1. shell脚本练习02--求字符串的长度

    ######################################################################### # File Name: -.sh # Author ...

  2. 18-2-call和apply

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. js 实现vue—引入子组件props传参

    参考:https://www.cnblogs.com/xiaohuochai/p/7388866.html 效果 html <!DOCTYPE html> <html> < ...

  4. java_static关键字

    /** * static关键字:静态关键字 * 静态优先于非静态加载到内存中(静态优先于对进入到内存中) * 被static修饰的成员变量不能被序列化的,序列化的都是对象 * transient关键字 ...

  5. LeetCode 38.报数(Python3)

    题目: 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 被读作  "one 1& ...

  6. LINUX常用操作命令和命令行编辑快捷键

    终端快捷键: Ctrl + a/Home 切换到命令行开始 Ctrl + e/End 切换到命令行末尾 Ctrl + l 清除屏幕内容,效果等同于clear Ctrl + u 清除剪切光标之前的内容 ...

  7. 【默默努力】fishingGame

    这个捕鱼游戏挺有意思的,通过发射子弹,打鱼.打鱼的子弹会消耗金币,但是打鱼如果打到了鱼,就会奖励金币的数量. 我如果写这个的话,应该会画一个 背景海底,然后生成很多鱼的图片,还要有一个大炮,金币.大炮 ...

  8. 【默默努力】vue-pc-app

    最近在github上面看到了一个团队的项目,真的非常赞.他们进行vue-cli的二次开发,将项目用自己的方式打包. 今天的这个开源项目地址为:https://github.com/tffe-team/ ...

  9. bat删除多少天前的文件包含子目录

    通过 Forfiles 删除指定目录下过期的备份文件 /*-- 用法详解 D:/>forfiles /? FORFILES [/P pathname] [/M searchmask] [/S] ...

  10. 【BZOJ4916】神犇与蒟蒻

    题面 Description 很久很久以前,有一只神犇叫yzy; 很久很久之后,有一只蒟蒻叫lty; Input 请你读入一个整数N;\(1<=N<=10^9\),A.B模\(10^9+7 ...