429. N-ary Tree Level Order Traversal**

https://leetcode.com/problems/n-ary-tree-level-order-traversal/

题目描述

Given an n-ary tree, return the level order traversal of its nodes’ values.

Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).

Example 1:

Input: root = [1,null,3,2,4,null,5,6]
Output: [[1],[3,2,4],[5,6]]

Example 2:

Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
Output: [[1],[2,3,4,5],[6,7,8,9,10],[11,12,13],[14]]

Constraints:

  • The height of the n-ary tree is less than or equal to 1000
  • The total number of nodes is between [0, 10^4]

C++ 实现 1

层序遍历使用队列来实现.

/*
// Definition for a Node.
class Node {
public:int val;vector<Node*> children;Node() {}Node(int _val) {val = _val;}Node(int _val, vector<Node*> _children) {val = _val;children = _children;}
};
*/
class Solution {public:vector<vector<int>> levelOrder(Node* root) {if (!root) return {};queue<Node*> q;q.push(root);vector<vector<int>> res;while (!q.empty()) {auto size = q.size();vector<int> tmp;while (size --) {auto r = q.front();q.pop();tmp.push_back(r->val);for (auto &c : r->children)q.push(c);}res.push_back(tmp);}return res;}
};

429. N-ary Tree Level Order Traversal**相关推荐

  1. 429. N-ary Tree Level Order Traversal - LeetCode

    Question 429. N-ary Tree Level Order Traversal Solution 题目大意: N叉树,返回每层的值,从上到下,从左到右 思路: 利用队列遍历这个N叉树 J ...

  2. 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 ...

  3. 102. Binary Tree Level Order Traversal

    题目 Binary Tree Level Order Traversal 层次遍历二叉树 链接 Given a binary tree, return the level order traversa ...

  4. 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 ...

  5. Binary Tree Level Order Traversal II 解题思路

    思路: 与Binary Tree Level Order Traversal I 几乎一样.只是最后将结果存放在栈里,然后在栈里再传给向量即可. 再次总结思路: 两个queue,先把第一个放进q1,循 ...

  6. [LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)

    目录: 1.Binary Tree Level Order Traversal - 二叉树层次遍历 BFS 2.Binary Tree Level Order Traversal II - 二叉树层次 ...

  7. LeetCode 107. Binary Tree Level Order Traversal II

    LeetCode 107. Binary Tree Level Order Traversal II Solution1:我的答案 比102那道题多了一行代码... /*** Definition f ...

  8. 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 ...

  9. 【Binary Tree Level Order Traversal】cpp

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

最新文章

  1. 湖南工大计算机专业咋样,西北工业大学还是湖南大学计算机
  2. 开始我的c++学习之路
  3. 利用SoapUI 测试web service的方法介绍
  4. 图像基本处理算法的简单实现(二)
  5. 环形buffer代码_为什么Buffer开发人员开源了他的代码
  6. 树状数组相关应用之平面范围求和问题
  7. 数据库原理—数据库基础(二)
  8. 进程全家桶,看这一篇就够了 | 原力计划
  9. 体验Windows server 2012上安装SQL 2012
  10. java的第一行代码
  11. Blk read/s Blk wrtn/s Blk read Blk wrtn分别代表什么意思
  12. 【机器人学】机器人开源项目KDL源码学习:(10)KDL中的OOP思想---继承
  13. MATLAB求解一阶RC电路和二阶RLC电路
  14. 【壁上观】AMD ZEN将至能战8核i7 Intel慌不慌?
  15. Vue 项目如何进行 SEO 优化
  16. web字体 衬线字体与非衬线字体区别 字体扫盲
  17. centos安装mysql5.7.19报 error while loading shared libraries: libaio.so.1
  18. 云豹智能发布全功能云霄DPU网卡,引领数据中心新趋势
  19. 企业微信会员销售额达40%的资生堂,能带给我们怎样的数字化增长启示?
  20. securecrt连接不上vmware

热门文章

  1. 怎么把一张普通照片变成证件照?这样做其实非常简单
  2. 阿呆喵广告过滤 v1.9.0.1 官网版
  3. SQL Server 2005系列教学(13) 游标
  4. 下载中转站downloadTransport
  5. Arcgis属性表字段值批量替换
  6. 坪山体育中心体育馆全景不同高度展示
  7. 世界之窗如何保存html,导入其他书签
  8. SpringMVC基础一
  9. Opencv4(C++)实战案例1:将朦胧的图像变清晰(去雾)
  10. Python学记(三)turtle库