当前位置:网站首页>在每个树行中找最大值[分层遍历之一的扩展]
在每个树行中找最大值[分层遍历之一的扩展]
2022-06-24 19:28:00 【REN_林森】
前言
树的层次遍历作为遍历基本方式之一,也是很多问题的基础。
分层遍历不同于层次遍历,在于需要主动分层。
分层遍历有两种情况,用size分层;还有种二维数组的,可递归+level来分层。
掌握了分层遍历,对于分层遍历的扩展就很容易解决了,如每行的最大值,用一个遍历记录每行的最大值即可。
一、在每个树行中找最大值

二、分层遍历扩展
package everyday.medium;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
// 在每个树行中找最大值
public class LargestValues {
/* target:找到每一层的最大值。 分层遍历 + 每层记录一个最大值,一层遍历完后加入list */
public List<Integer> largestValues(TreeNode root) {
List<Integer> ans = new ArrayList<>();
// 特殊情况,特殊处理。
if (null == root) return ans;
// 用队列+size来做到分层遍历,注:不是层次遍历。
// 习惯用双端队列,毕竟功能强大,但是有却用不着时也是一种资源浪费。
ArrayDeque<TreeNode> que = new ArrayDeque<>();
que.add(root);
int size = que.size();
int max = 1 << 31;
while (!que.isEmpty()) {
TreeNode node = que.poll();
// 加入下层节点。
if (null != node.left) que.offer(node.left);
if (null != node.right) que.offer(node.right);
// 记录该层最大值。
max = Math.max(max, node.val);
// 用size分层
if (0 == --size) {
// 加入该层最大值。
ans.add(max);
// 更新下一层的最大值。
max = 1 << 31;
// 更新下一层的size
size = que.size();
}
}
return ans;
}
// Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
}
总结
1)分层遍历有两种情况,一种队列+size分层,一种递归+level分层。
参考文献
边栏推荐
- Handwritten RPC the next day -- review of some knowledge
- Implementation of adjacency table storage array of graph
- Network layer & IP
- VirtualBox virtual machine installation win10 Enterprise Edition
- Data link layer & some other protocols or technologies
- 关于Unity中的transform.InverseTransformPoint, transform.InverseTransofrmDirection
- 力扣每日一题-第26天-496.下一个更大元素Ⅰ
- CondaValueError: The target prefix is the base prefix. Aborting.
- Byte software testing basin friends, you can change jobs. Is this still the byte you are thinking about?
- Pattern recognition - 0 introduction
猜你喜欢

Bld3 getting started UI

LeetCode-513. 找树左下角的值

关于Unity中的transform.InverseTransformPoint, transform.InverseTransofrmDirection

Football information query system based on C language course report + project source code + demo ppt+ project screenshot

Antdb database online training has started! More flexible, professional and rich

CondaValueError: The target prefix is the base prefix. Aborting.

Introduce the overall process of bootloader, PM, kernel and system startup

Memcached full profiling – 1 Fundamentals of memcached

Static routing experiment

福建省发改委福州市营商办莅临育润大健康事业部指导视察工作
随机推荐
CondaValueError: The target prefix is the base prefix. Aborting.
memcached完全剖析–1. memcached的基础
Interpretation of ebpf sockops code
how to install clustershell
力扣每日一题-第25天-496.下一个更大元素Ⅰ
Byte software testing basin friends, you can change jobs. Is this still the byte you are thinking about?
Notes_ Vlan
Network layer & IP
VirtualBox virtual machine installation win10 Enterprise Edition
Blender's landscape
The first day of handwritten RPC -- review of some basic knowledge
C语言-关键字1
The virtual currency evaporated $2trillion in seven months, and the "musks" ended the dream of 150000 people becoming rich
About transform InverseTransformPoint, transform. InverseTransofrmDirection
【吴恩达笔记】卷积神经网络
Memcached comprehensive analysis – 2 Understand memcached memory storage
使用 Go 编程语言 66 个陷阱:Golang 开发者的陷阱和常见错误指北
手动事务的几个类
Shengzhe technology AI intelligent drowning prevention service launched
Volcano成Spark默认batch调度器