题目内容

题目分析

这是一道典型的树结构遍历题,可以使用层序遍历(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. Java高并发,ArrayList、HashSet、HashMap不安全的集合类

    首先是我们的ArrayList: 这次我们讲解的是集合的不安全,首先我们都知道ArrayList吧! List<String> list=new ArrayList<>(); ...

  2. Win11安装基于WSL2的Ubuntu

    1. 概述 趁着还没有完全忘记,详细记录一下在Win11下安装基于WSL2的Ubuntu的详细过程.不得不说WLS2现在被微软开发的比较强大了,还是很值得安装和使用的,笔者就通过WLS2安装的Ubun ...

  3. 【填算符】(log 值域的做法)

    比赛在这里呢 填算符 下发题解说的神马东西,赛时根本想不到 讲一个赛时想得到的 \(O(n\log 值域)\) 的思路,很好理解 我们处理出二进制下每一位上的 1 的最后一次出现的位置,将第 \(i\ ...

  4. 题解: CF768D Jon and Orbs

    题解:CF768D Jon and Orbs 一句话题面:有k种不同的物品,每天等概率任取一种(不一定是新的种类).q组询问,每组给出一个p,问取完这k件物品的概率不小于\(\frac{p}{2000 ...

  5. CubeIDE 主题美化与颜色设置

    一.主题美化 搜索引擎里很多,这里不必多说. 二.颜色设置 2.1.关于控制台 菜单栏里:window→preference→输入"console"并回车,然后按照下图指示来: 2 ...

  6. ChatGPT:编程的 “蜜糖” 还是 “砒霜”?告别依赖,拥抱自主编程的秘籍在此!

    在当今编程界,ChatGPT 就像一颗耀眼却又颇具争议的新星,它对编程有着不可忽视的影响.但这影响就像一把双刃剑,使用不当,就可能让我们在编程之路上"受伤". 一.过度依赖 Cha ...

  7. ECharts 引入中国地图和区域地图

    一,引入中国地图   <div  id="chinaMap"></div> import china from 'echarts/map/js/china. ...

  8. vue-elementui中el-table跨页选择和v-if导致列错乱/选择框无法显示

    在vue-elementui中使用el-table,当type="selection"的时候,分页数据进行不同页跳转选择 需要这种功能的时候我们需要在el-table的标签上为每个 ...

  9. 使用SpringSecurity3实现RBAC权限管理

    1. What? 什么是权限管理? 具体可参见百度:http://baike.baidu.com/view/2108713.htm 名词备注: 数据级权限:百科内的权限管理一文解释的比较不错,但其中的 ...

  10. Jenkins之插件汇总

    Nodejs: 构建前端项目或Node项目 Build Name and Description Setter Publish over SSH: 远程执行shell命令 Blue Ocean   友 ...