当前位置:网站首页>429. N 叉树的层序遍历(两种解法)
429. N 叉树的层序遍历(两种解法)
2022-07-30 05:15:00 【Alex_Drag】
N 叉树的层序遍历
给定一个 N 叉树,返回其节点值的层序遍历。(即从左到右,逐层遍历)。
树的序列化输入是用层序遍历,每组子节点都由 null 值分隔(参见示例)。
示例 1:

输入:root = [1,null,3,2,4,null,5,6]
输出:[[1],[3,2,4],[5,6]]
示例 2:
输入: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]
输出:[[1],[2,3,4,5],[6,7,8,9,10],[11,12,13],[14]]
提示:
树的高度不会超过 1000
树的节点总数在 [0, 10^4] 之间
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/n-ary-tree-level-order-traversal
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
第一种解法:使用队列
将一层的节点都存入到队列,然后记录当前长度,一一弹出在这一长度中队列的节点,弹出的节点中,如果有节点的children不为空的,则将该节点的子节点添加到队列中,进行下一层级遍历。
public List<List<Integer>> levelOrder(Node root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) {
return res;
}
Queue<Node> queue = new LinkedList<>();
queue.add(root);
while(!queue.isEmpty()) {
int size = queue.size();
List<Integer> cur = new ArrayList<>();
int i = -1;
while (++i < size) {
Node node = queue.poll();
cur.add(node.val);
if (node.children != null) {
queue.addAll(node.children);
}
}
res.add(cur);
}
return res;
}
第二种解法:深度遍历
将当前的层数传入进方法中,如果当前的层数等于res的的长度,代表之前未访问过当前层,创建一个集合,继续递归就可以了。
public List<List<Integer>> levelOrder(Node root) {
List<List<Integer>> res = new ArrayList<>();
dfs(root, 0, res);
return res;
}
private void dfs(Node node, int level, List<List<Integer>> res) {
if (node == null) {
return;
}
if (res.size() == level) {
res.add(new ArrayList<>());
}
res.get(level).add(node.val);
if (node.children != null) {
for(Node next : node.children) {
dfs(next, level + 1, res);
}
}
}
边栏推荐
- 丑陋的程序员
- Seata异常:endpoint format should like ip:port
- Codeforces Round #809 (Div. 2) A~D
- Predictive maintenance scheduling of multiple power equipment based on data-driven fault prediction
- Docker-compose安装mysql
- 从想当亿万富翁到职场、创业、爱情、抑郁、学医学武,我的程序人生
- Discourse Custom Header Links
- 22. Why do you need a message queue?What are the advantages of using the message queue?
- (RCE) Remote Code/Command Execution Vulnerability Vulnerability Exercise
- 解读 Kylin 3.0.0 | 更敏捷、更高效的 OLAP 引擎
猜你喜欢

Go语学习笔记 - gorm使用 - 事务操作 Web框架Gin(十一)

Usage when saving pointers in std::vector

即刻报名|如何降低云上数据分析成本?

力扣541-反转字符串2——双指针法

【Redis高手修炼之路】Jedis——Jedis的基本使用

22-07-29 西安 分布式事务、Seata

Within the SQL connection table (link connections, left or right, cross connection, full outer join)

Kyligence 出席华为全球智慧金融峰会,加速拓展全球市场

Internet (software) company project management software research report

即刻报名|前沿技术探索:如何让 Spark 更强劲、更灵活
随机推荐
std::vector中保存指针时用法
力扣05-替换空格——字符串问题
Hexagon_V65_Programmers_Reference_Manual (11)
Programmers care guide, give yourself a chance to make the occasional relaxation of body and mind
(RCE) Remote Code/Command Execution Vulnerability Vulnerability Exercise
力扣1047-删除字符串中的所有相邻重复项——栈
Acwing perfect number
剑指offer(刷题篇12)
Record of problems encountered by the pyinstaller packager
go language study notes 2
黄金圈法则:成功者必备的深度思考方法
容器化 | 在 K8s 上部署 RadonDB MySQL Operator 和集群
翻译 | Kubernetes 将改变数据库的管理方式
pycharm上的tensorflow环境搭载
22. Why do you need a message queue?What are the advantages of using the message queue?
MySql string splitting realizes the split function (field splitting, column switching, row switching)
Codeforces Round #809 (Div. 2) A~D
互联网(软件)公司项目管理软件调研报告
参与开源,让程序员找回热血和激情
RadonDB MySQL on K8s 2.1.4 发布!