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

515. Find Largest Value in Each Tree Row

题目描述

You need to find the largest value in each row of a binary tree.

您需要在二叉树的每一行中找到最大的值。

LeetCode515. Find Largest Value in Each Tree Row

Example:

Input:
```
1
/ \
3 2
/ \ \
5 3 9
```
Output: [1, 3, 9]

Java 实现

TreeNode Class

class TreeNode {
int val;
TreeNode left;
TreeNode right; TreeNode(int x) {
val = x;
}
}
import java.util.LinkedList;
import java.util.List; class Solution {
public List<Integer> largestValues(TreeNode root) {
List<Integer> res = new LinkedList<>();
helper(root, res, 0);
return res;
} public void helper(TreeNode root, List<Integer> res, int depth) {
if (root == null) {
return;
}
if (depth == res.size()) {
res.add(root.val);
} else {
res.set(depth, Math.max(res.get(depth), root.val));
}
helper(root.left, res, depth + 1);
helper(root.right, res, depth + 1);
}
}

参考资料

LeetCode 515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)的更多相关文章

  1. [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 ...

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

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

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

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

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

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

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

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

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

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

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

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

  8. LN : leetcode 515 Find Largest Value in Each Tree Row

    lc 515 Find Largest Value in Each Tree Row 515 Find Largest Value in Each Tree Row You need to find ...

  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 ...

随机推荐

  1. CF1209题解

    E 每列都可以沿下滚动若干次,使得各行最大值之和最大 对每列的元素计算最大值,降序排,显然取前\(min(n,m)\)个列处理即可 比较巧妙的动规,设\(f(i,S)\)为前\(i\)列,已经确定最大 ...

  2. Redis面试问答(子文章)(持续更新)

    -----> 总文章 入口 文章目录 [-----> 总文章 入口](https://blog.csdn.net/qq_37214567/article/details/90174445) ...

  3. Requests库的主要方法:requests.request为requests.get和requests.post两个的汇总,只是需要传方法

    1. requests.request(method,url,**kwargs) method:请求方式,对应get/put/post等七种 :拟获取页面的url链接 :控制访问参数,共13个 met ...

  4. ubuntu之路——day10.1 ML的整体策略——正交化

    orthogonalization 正交化的概念就是指,将你可以调整的参数设置在不同的正交的维度上,调整其中一个参数,不会或几乎不会影响其他维度上的参数变化,这样在机器学习项目中,可以让你更容易更快速 ...

  5. linux xlearn安装

     机器学习中的又一个利器,广泛用于Kaggle或类似的数据比赛. xlearn的优势: 1.通用性好,包括主流的算法(lr, fm, ffm 等),用户不用再切换于不同软件之间 2.性能好,测试 xL ...

  6. openssl从内存中读取私钥进行签名

    麻痹的找了好久,真恶心! #include <stdio.h> #include <stdlib.h> #ifdef WIN32 #include <windows.h& ...

  7. LF: 换行,U+000A VT: 垂直定位,U+000B FF: 换页符,U+000C CR: 回车符,U+000D CR+LF:CR(U+000D)后跟LF(U+000A) NEL: 下一行,U+0085 LS: 分行,U+2028 PS: 分段,U+2029

    https://zh.wikipedia.org/wiki/換行 换行(英语:newline.line ending.end-of-line (EOL).line Feed (LF).line bre ...

  8. 关于postman与shiro权限验证问题

    作为一个java的开发小白 , 写完一个web方法测试是必不可少的 , 只有测试号没问题的方法给别人时 ,别人才不知道你是小白 , 要不然很尴尬的 .新手入坑的测试工具是postman .这个工具用起 ...

  9. Looper: Looper,Handler,MessageQueue三者之间的联系

    在Android中每个应用的UI线程是被保护的,不能在UI线程中进行耗时的操作,其他的子线程也不能直接进行UI操作.为了达到这个目的Android设计了handler Looper这个系统框架,And ...

  10. 002——Angular 目录结构分析、app.module.ts 详解、以及 Angular 中创建组件、组件 详解、 绑定数据

    一.目录结构分析 二. app.module.ts.组件分析 1.app.module.ts 定义 AppModule,这个根模块会告诉 Angular 如何组装该应用. 目前,它只声明了 AppCo ...