题目内容

题目分析

这是一道典型的树结构遍历题,可以使用层序遍历(BFS)或者(DFS)进行解题。

  • 在BFS中,使用队列结构遍历树的每一层并维护每层的最大值。
  • 在DFS中,由于并不是一层一层的去访问树的节点,因此需要使用HashMap来维护每个层最大值。
BFS
    public List<Integer> largestValues(TreeNode root) {
Deque<TreeNode> queue = new ArrayDeque();
if (root != null) queue.add(root);
List<Integer> res = new ArrayList();
while(!queue.isEmpty()){
int sz = queue.size();
Integer levelMax = Integer.MIN_VALUE;
for(int i = 0; i < sz; i++){
TreeNode node = queue.pollFirst();
levelMax = Math.max(levelMax, node.val);
if (node.left != null) queue.addLast(node.left);
if (node.right != null) queue.addLast(node.right);
}
res.add(levelMax);
}
return res;
}
DFS
    int maxDepth = 0;
Map<Integer, Integer> map = new HashMap<>();
public List<Integer> largestValues(TreeNode root) {
List<Integer> res = new ArrayList<>();
dfs(root, 1);
for (int i = 1; i <= max; i++) res.add(map.get(i));
return res;
}
void dfs(TreeNode node, int depth) {
if (node == null) return ;
maxDepth = Math.max(maxDepth, depth);
map.put(depth, Math.max(map.getOrDefault(depth, Integer.MIN_VALUE), node.val));
dfs(node.left, depth + 1);
dfs(node.right, depth + 1);
}

[LC515]在每个树的行中找最大值的更多相关文章

  1. Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)

    Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row) 深度优先搜索的解题详细介绍,点击 您需要在二叉树 ...

  2. LeetCode 515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)

    515. 在每个树行中找最大值 515. Find Largest Value in Each Tree Row 题目描述 You need to find the largest value in ...

  3. Java实现 LeetCode 515 在每个树行中找最大值

    515. 在每个树行中找最大值 您需要在二叉树的每一行中找到最大的值. 示例: 输入: 1 / \ 3 2 / \ \ 5 3 9 输出: [1, 3, 9] /** * Definition for ...

  4. [Swift]LeetCode515. 在每个树行中找最大值 | Find Largest Value in Each Tree Row

    You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...

  5. Leetcode 515. 在每个树行中找最大值

    题目链接 https://leetcode-cn.com/problems/find-largest-value-in-each-tree-row/description/ 题目描述 您需要在二叉树的 ...

  6. 515 Find Largest Value in Each Tree Row 在每个树行中找最大值

    在二叉树的每一行中找到最大的值.示例:输入:           1         /  \        3   2       /  \    \        5   3    9 输出: [ ...

  7. C语言递归之在每个树行中找最大值

    题目描述 您需要在二叉树的每一行中找到最大的值. 示例 输入: / \ / \ \ 输出: [, , ] 题目要求 /** * Definition for a binary tree node. * ...

  8. Leetcode515. Find Largest Value in Each Tree Row在每个树行中找最大值

    您需要在二叉树的每一行中找到最大的值. 示例: 输入: 1 / \ 3 2 / \ \ 5 3 9 输出: [1, 3, 9] class Solution { public: vector<i ...

  9. [LeetCode] Find Largest Value in Each Tree Row 找树每行最大的结点值

    You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...

  10. 命令行中运行Java字节码文件提示找不到或无法加载主类的问题

    测试类在命令行操作,编译通过,运行时,提示 错误: 找不到或无法加载主类 java类 package com.company.schoolExercise; public class test7_3_ ...

随机推荐

  1. 自建家庭 KTV,在家想嗨就嗨

    现在用户最多.曲库最多的 K 歌软件是全民K歌,基本上想唱的歌都有,而且基本上每首歌都有 MV 或视频,使用体验也还不错,但是收费太贵了,对于一个月唱不了几次的打工人来说,唱一首歌就是"天价 ...

  2. SpringBoot数据访问,配置数据源

    配置文件目录 配置数据源jdbc版本 JDBC(.tomcat.jdbc.pool.DataSource作为数据源) <?xml version="1.0" encoding ...

  3. win10下端口映射设置内网别人访问本机安装的vmware默认NAT网络

    用管理员权限打开powershell或者cmd,命令如下 netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=主 ...

  4. [NOIP2022] 比赛 随机排列 部分分

    看到最大值,考虑使用单调栈搞出 \([la_i, ra_i], [lb_i, rb_i]\) 表示这一段区间 \(i\) 是 \(a, b\) 的最大值.预处理是简单的. inline void in ...

  5. 13-1 c++拷贝控制:拷贝赋值与销毁

    定义一个类时,我们必须对它进行拷贝控制,即控制该类在进行拷贝.赋值.移动和销毁时要进行哪些操作 一个类通过五个特殊的成员函数进行拷贝控制 拷贝构造函数 拷贝赋值函数 移动构造函数 移动赋值函数 析构函 ...

  6. Linux Shell简介

    目录 Shell是什么 基本介绍 用Shell编写HelloWorld Shell是什么 Shell是一个命令行解释器,它为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以 ...

  7. 9.Kubernetes核心技术-Controller

    Kubernetes核心技术-Controller 内容 什么是Controller Pod和Controller的关系 Deployment控制器应用场景 yaml文件字段说明 Deployment ...

  8. Help document of CAD Plus Mobile

    Help document for Mac 中文使用帮助 If you have any questions, please send email to 3167292926@qq.com 1. Pe ...

  9. 低功耗4G模组:tcs3472颜色传感器示例

    ​ 今天我们学习合宙低功耗4G模组Air780EP的LuatOS开发tcs3472示例,文末[阅读原文]获取最新资料1 一.简介 tcs3472颜色传感器能够读取照射到的物体的RGB三种数值,从而识别 ...

  10. MySQL原理简介—9.MySQL索引原理

    大纲 1.磁盘数据页的存储结构 2.没有索引数据库如何搜索数据 3.在表中插入数据时如何进行页分裂 4.如何设计主键索引及如何根据主键索引查询 5.索引的物理存储结构 6.更新数据时自动维护的聚簇索引 ...