[LC515]在每个树的行中找最大值
题目内容

题目分析
这是一道典型的树结构遍历题,可以使用层序遍历(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]在每个树的行中找最大值的更多相关文章
- Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)
Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row) 深度优先搜索的解题详细介绍,点击 您需要在二叉树 ...
- 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 ...
- Java实现 LeetCode 515 在每个树行中找最大值
515. 在每个树行中找最大值 您需要在二叉树的每一行中找到最大的值. 示例: 输入: 1 / \ 3 2 / \ \ 5 3 9 输出: [1, 3, 9] /** * Definition for ...
- [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 ...
- Leetcode 515. 在每个树行中找最大值
题目链接 https://leetcode-cn.com/problems/find-largest-value-in-each-tree-row/description/ 题目描述 您需要在二叉树的 ...
- 515 Find Largest Value in Each Tree Row 在每个树行中找最大值
在二叉树的每一行中找到最大的值.示例:输入: 1 / \ 3 2 / \ \ 5 3 9 输出: [ ...
- C语言递归之在每个树行中找最大值
题目描述 您需要在二叉树的每一行中找到最大的值. 示例 输入: / \ / \ \ 输出: [, , ] 题目要求 /** * Definition for a binary tree node. * ...
- Leetcode515. Find Largest Value in Each Tree Row在每个树行中找最大值
您需要在二叉树的每一行中找到最大的值. 示例: 输入: 1 / \ 3 2 / \ \ 5 3 9 输出: [1, 3, 9] class Solution { public: vector<i ...
- [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 ...
- 命令行中运行Java字节码文件提示找不到或无法加载主类的问题
测试类在命令行操作,编译通过,运行时,提示 错误: 找不到或无法加载主类 java类 package com.company.schoolExercise; public class test7_3_ ...
随机推荐
- Java基础综合项目(ATM系统)
文章目录 1.ATM项目介绍 2.项目架构搭建.欢迎界面设计 3.开户功能实现 4.生成卡号 5.登录功能 6.展示用户操作界面 7.查询账户.退出 8.存款 9.取款 10.转账 11.修改密码 1 ...
- 深度学习入门笔记——DataLoader的使用
如何使用数据集DataSet? 在介绍DataLoader之前,需要先了解数据集DataSet的使用.Pytorch中集成了很多已经处理好的数据集,在pytorch的torchvision.torch ...
- Centos7下Docker搭建Matomo
1.docker安装和启动 wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repo ...
- 一款.NET开源的屏幕实时翻译工具
前言 今天大姚给大家推荐一款.NET开源的屏幕实时翻译工具:Translumo. 工具介绍 Translumo是一个.NET开源的高级屏幕翻译工具,能够实时检测和翻译选定区域内的文本(如字幕).Tra ...
- 如何在原生鸿蒙中进行RN热加载
一.背景 在上一篇博客中,我分享了将RN的bundle包在原生鸿蒙开发中进行使用.但是如果我们在实际的开发过程中,每次修改完代码都需要打包,然后重新运行原生项目的话效率就有点太低了. 原生鸿蒙支持RN ...
- gal game 杂谈——前言
gal game 杂谈--前言 大年三十凌晨(早上)打算开始写了吧,作为第一篇先写一些前言好了. 第一次接触gal game还是在B站上看到有人玩<我和她的世界末日>当时觉得挺有意思的,加 ...
- 使用Ollama和Open WebUI管理本地开源大模型
Open WebUI和Ollama介绍 Open WebUI 是一个功能丰富且用户友好的自托管 Web 用户界面(WebUI),它被设计用于与大型语言模型(LLMs)进行交互,特别是那些由 Ollam ...
- VUE懒加载的table前端搜索
// 前端搜索 fliterData() { const search = this.search if (search) { this.blist = this.list.filter(item = ...
- 曾经做的一个JS小游戏——《Battle City》
今天改网盘密码时,找到了个很久前的东西:JavaScript版的坦克大战.07年的夏天制作花了好多个夜晚制作,那段着迷JS游戏的疯狂时光.但因为后来众多浏览器的出现,导致了游戏兼容性大大的下降,最终放 ...
- 在table中,tbody没有充满整个table
解决方法就是给table加上 display:table;就好了