题目内容

题目分析

这是一道典型的树结构遍历题,可以使用层序遍历(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. 一文彻底搞定Redis与MySQL的数据同步

    Redis 和 MySQL 一致性问题是企业级应用中常见的挑战之一,特别是在高并发.高可用的场景下.由于 Redis 是内存型数据库,具备极高的读写速度,而 MySQL 作为持久化数据库,通常用于数据 ...

  2. Machine Learning Week_7 Support Vector Machines

    目录 1 Large Margin Classification 1.1 Optimization Objective 1.1 Logistic Regresson 1.2 Cost 1.3 Supp ...

  3. JS转义html编码

    两个方法: 1.利用用浏览器内部转换器实现html转义: 2.用正则表达式实现html转义: var HtmlUtil = { /*1.用浏览器内部转换器实现html编码(转义)*/ htmlEnco ...

  4. SSIS ODBC方式连接mysql数据库

    系统环境:WIN 10 64位 1.安装Mysql odbc connector 插件 文章说明链接: https://www.cnblogs.com/santiagoMunez/p/4780301. ...

  5. Rust 版本一直是 1.4 或者其它版本

    Rust 版本一直是 1.4 或者其它版本 通过rustup update 升级或者 rustup default 设置版本也不行 解决方法 删除 rust-toolchain 这个东西,这个东西覆盖 ...

  6. 卡特兰数 Catalan 数列

    卡特兰数 Catalan 数列 引入 有一个无限大的栈,进栈的顺序为 \(1,2,\cdots,n\),求有多少种不同的出栈序列. 设 \(h[n]\) 为 \(n\) 个数的出栈序列方案数. 可以这 ...

  7. php xattr操作文件扩展属性后续

    由于之前看了xattr的写入效率,这里简单的实现一下生产者消费者模型的高速写入. 生产者(让他创建40万条数据) <?php // 生产者 不断的生产大量数据 但是总会有停止的时候(本业务功能结 ...

  8. element-ui resetFields 无效的问题

    1.问题 触发bug的条件是先打开,编辑进行赋值,后打开新增 先点开编辑 再打开新增 这个时候你会发现刚刚赋值过的数据还遗留在表单里面 即使在打开  dialog  的时候执行了重置也没有效果 res ...

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

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

  10. 解读vue的webpack.base.conf.js配置

    'use strict' // 引入nodejs路径模块 const path = require('path') // 引入utils工具模块,utils主要用来处理css-loader和vue-s ...