原题

思路:

bfs,每一层遍历一次加到一个vector,同时把该点的子元素加到queue中。

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> v;
int len = q.size();
for (int i = 0; i < len; i++)
{
Node *temp = q.front();
q.pop();
v.push_back(temp->val);
for (auto &a : temp->children)
{
q.push(a);
}
}
res.push_back(v);
}
return res;
}
};

[leetcode ]429. N-ary Tree Level Order Traversale (easy)的更多相关文章

  1. 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  2. [LeetCode] 429. N-ary Tree Level Order Traversal_ Easy

    Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  3. 【LeetCode】107. Binary Tree Level Order Traversal II (2 solutions)

    Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal ...

  4. 【LeetCode】102. Binary Tree Level Order Traversal (2 solutions)

    Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...

  5. 【一天一道LeetCode】#102. Binary Tree Level Order Traversal

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  6. 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...

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

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

  8. 【LeetCode OJ】Binary Tree Level Order Traversal

    Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ Traverse the tree ...

  9. 【LeetCode OJ】Binary Tree Level Order Traversal II

    Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ Use BFS from th ...

随机推荐

  1. Another maybe monad library for ruby

    欢迎任何形式的转载,但请务必注明出处:http://www.cnblogs.com/liangjingyang 项目地址:https://github.com/liangjingyang/maybe_ ...

  2. 我们检测到您的浏览器不兼容传输层安全协议 (TLS) 1.1 或更高版本,此协议在 Internet 上用于确保您通信的安全性。

    早上使用.Net WebClient类采集亚马逊数据,返回http 400 Bad Request错误,内容里面有“我们检测到您的浏览器不兼容传输层安全协议 (TLS) 1.1 或更高版本,此协议在 ...

  3. Java基础(六) static五大应用场景

    static和final是两个我们必须掌握的关键字.不同于其他关键字,他们都有多种用法,而且在一定环境下使用,可以提高程序的运行性能,优化程序的结构.上一个章节我们讲了final关键字的原理及用法,本 ...

  4. Tido 习题-二叉树-树状数组实现

    题目描述 这就是一个简单的树状数组入门题 可以动态地进行区间和查询 随时可能会进行更新   #include<iostream> #include<cstdio> #inclu ...

  5. 曹工说Tomcat3:深入理解 Tomcat Digester

    一.前言 我写博客主要靠自己实战,理论知识不是很强,要全面介绍Tomcat Digester,还是需要一定的理论功底.翻阅了一些介绍 Digester 的书籍.博客,发现不是很系统,最后发现还是官方文 ...

  6. java文件操作和文件过滤器

    文件操作 java提供了File类来供我们操作文件,FIle是文件或文件夹的抽象表示,也就是对象表示.总结了几个常用的方法 File(File parenrt,String child).FIle(S ...

  7. yii中find()指定条件

    find()查找指定的条件 $modelName::model->find(); 使用条件对象 $criteria = new CDbCriteria(); $criteria->sele ...

  8. Java学习笔记——设计模式之十.观察者模式

     观察者模式(Observer),定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象.这个主题对象在状态发生变化时,会通知所有观察者对象,使他们能够自动更新自己. Subject类: ...

  9. 深入V8引擎-AST(4)

    (再声明一下,为了简单暴力的讲解AST的转换过程,这里的编译内容以"'Hello' + ' World'"作为案例) 上一篇基本上花了一整篇讲完了scanner的Init方法,接下 ...

  10. Python连载21-collections模块

    一.collections模块 1.函数namedtuple (1)作用:tuple类型,是一个可命名的tuple (2)格式:collections(列表名称,列表) (3)​返回值:一个含有列表的 ...