(N叉树 BFS) leetcode429. N-ary Tree Level Order Traversal
Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example, given a 3-ary tree:

We should return its level order traversal:
[
[1],
[3,2,4],
[5,6]
]
Note:
- The depth of the tree is at most
1000. - The total number of nodes is at most
5000.
-----------------------------------------------------------------------------------------------------------------------------------
这个层序遍历,自然用BFS写。和二叉树的层序遍历类似,连代码不会相差很大。
C++代码:
/*
// 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) return {};
queue<Node*> q;
q.push(root);
vector<vector<int> > vec;
while(!q.empty()){
vector<int> vec1;
int ans = q.size();
for(int i = ans;i > ; i--){
auto t = q.front();
q.pop();
vec1.push_back(t->val);
for(Node *cur:t->children){
q.push(cur);
}
}
vec.push_back(vec1);
}
return vec;
}
};
(N叉树 BFS) leetcode429. N-ary Tree Level Order Traversal的更多相关文章
- LeetCode 429. N叉树的层序遍历(N-ary Tree Level Order Traversal)
429. N叉树的层序遍历 429. N-ary Tree Level Order Traversal LeetCode429. N-ary Tree Level Order Traversal 题目 ...
- (二叉树 BFS) 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 left ...
- Leetcode之广度优先搜索(BFS)专题-详解429. N叉树的层序遍历(N-ary Tree Level Order Traversal)
Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree Level Order Traversal) 给定一个 N 叉树,返回其节点值的层序遍历. (即从左到右 ...
- [LeetCode] N-ary Tree Level Order Traversal N叉树层序遍历
Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode429. N-ary Tree Level Order Traversal
题目来源:429. N-ary Tree Level Order Traversal https://leetcode.com/problems/n-ary-tree-level-order-trav ...
- [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【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, ...
- 102. Binary Tree Level Order Traversal 广度优先遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- [LintCode] Binary Tree Level Order Traversal(二叉树的层次遍历)
描述 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分层遍历结果: [ [3] ...
- 【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 ...
随机推荐
- 阿里巴巴矢量图标库(Iconfont)-利于UI和前端的搭配
前端时间,做一个小网站的时候,需要用到很多小图标,UI设计好之后不知道如何使用,如果使用图片那会很麻烦,相信一些前端更喜欢iconfont这样的标签直接调用,这样包括颜色和大小以及使用都更方便快捷,于 ...
- swing Jframe 界面风格
用法:在jframe里面 UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel" ...
- Git 密钥对处理
生成密钥对: ssh-keygen -t rsa cd .ssh ls id_rsa 私钥 id_rsa.pub 公钥
- django 问题综合
orm部分 本篇文章我会持续更新,把开发中遇到的一切orm相关的问题都放在这里 mysql索引报错 使用django 的orm,数据库用的mysql,在使用makemigrations和migrate ...
- jQuery列表选择美化插件uichoose
项目主页:http://www.jq22.com/jquery-info12073 演示地址:http://www.jq22.com/yanshi12073
- ztree搜索节点并展开
web <div class="zTreeC"> <div class="searchL" lay-filter="searchL& ...
- cordova插件汇总
1.获取当前应用的版本号 cordova plugin add cordova-plugin-app-version 2.获取网络连接信息 cordova plugin add cordova-plu ...
- diskpart 的简单使用
- Thymeleaf入门(一)——入门与基本概述
一.概述 1.是什么 简单说, Thymeleaf 是一个跟 Velocity.FreeMarker 类似的模板引擎,它可以完全替代 JSP . 2.feature 1.Thymeleaf 在有网络和 ...
- PHP字符串函数、常量、数组排序
PHP字符串函数.常量.数组排序 strlen() 说明:strlen(),可以统计字符串长度 用途:strlen() 常用于循环和其他函数,在确定字符串何时结束很重要时.(例如,在循环中,我们也许需 ...