题目内容

题目分析

这是一道典型的树结构遍历题,可以使用层序遍历(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. Machine Learning Week_3 Classification Model

    目录 1 Classification and Representation 1.1 Classification unfamiliar words 1.2 Hypothesis Representa ...

  2. C# 13(.Net 9) 中的新特性 - 半自动属性

    C# 13 即 .Net 9 按照计划会在2024年11月发布,目前一些新特性已经定型,今天让我们来预览其中的一个新特性: 作者注:该特性虽然随着 C# 13 发布,但是仍然是处于 preview 状 ...

  3. P2540 [NOIP2015 提高组] 斗地主 加强版

    简要题意 给你一副手牌,求最少的次数出完所有手牌.(按照它给出的规定出) 题目 分析 因为求最小次数直接贪心很明显是错的,但又直接写不出 \(dp\) 的式子,所以我们只能够爆搜所有情况,但这样明显会 ...

  4. MongoDB聚合类操作

    MongoDB中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果.有点类似sql语句中的 count(*) 语法:db.tablename.aggregat ...

  5. weblogic历史漏洞

    weblogic历史漏洞 是什么?  weblogic是一个web服务器应用(中间件),和jboss一样都是javaee中间件,只能识别java语言,绝大部分漏洞都是T3反序列化漏洞  常见的中间件还 ...

  6. git pull发现有ahead of xx commits问题如何解决

    git pull 的时候发现有提示你ahead of xx commits 这个时候怎么办呢? 直接一句话定位到远程最新分支 git reset --hard origin/分支名称

  7. 添加linux alias

    有时候需要用alias做几个简单好记的命令方便快速输入 可以使用alias进行修改别名 在我的Ubuntu系统下 看一下 /etc/bash.bashrc 文件 在后面加入这个代码 # add the ...

  8. [昌哥IT课堂]|如何确定 MySQL 服务器是否为 LTS 版本(译)

    根据支持 LTS(长期支持)发布的新发布模型,给定的 MySQL 服务器将分为以下两类: 要么是 LTS 版本. 要么是创新版本. 本博客文章将解释如何确定给定的 MySQL 服务器是否为 LTS 版 ...

  9. apache+jk+tomcat集群+session同步

    apache2+tomcat5.5集群+session同步 作者:刘宇 liuyu.blog.51cto.com msn群:mgroup49073@hotmail.com (linuxtone)   ...

  10. zookeeper 分布式锁服务

    分布式锁服务在大家的项目中或许用的不多,因为大家都把排他放在数据库那一层来挡.当大量的行锁.表锁.事务充斥着数据库的时候.一般web应用很多的瓶颈都在数据库上,这里给大家介绍的是减轻数据库锁负担的一种 ...