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 each row of a binary tree.
您需要在二叉树的每一行中找到最大的值。
LeetCode515. Find Largest Value in Each Tree Row
Example:
```
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);
}
}
参考资料
- https://leetcode.com/problems/find-largest-value-in-each-tree-row/
- https://leetcode-cn.com/problems/find-largest-value-in-each-tree-row/
- https://leetcode.com/problems/find-largest-value-in-each-tree-row/discuss/98971/9ms-JAVA-DFS-solution
LeetCode 515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)的更多相关文章
- [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 ...
- Java实现 LeetCode 515 在每个树行中找最大值
515. 在每个树行中找最大值 您需要在二叉树的每一行中找到最大的值. 示例: 输入: 1 / \ 3 2 / \ \ 5 3 9 输出: [1, 3, 9] /** * Definition for ...
- Leetcode 515. 在每个树行中找最大值
题目链接 https://leetcode-cn.com/problems/find-largest-value-in-each-tree-row/description/ 题目描述 您需要在二叉树的 ...
- Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)
Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row) 深度优先搜索的解题详细介绍,点击 您需要在二叉树 ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- golang-切片
切片练习 package main import "fmt" /*func main() { arr := [10]int {1, 2, 3, 4, 5, 6, 7, 8, 9, ...
- 请解释或描述一下Django的架构
对于Django框架遵循MVC设计,并且有一个专有名词:MVT M全拼为Model,与MVC中的M功能相同,负责数据处理,内嵌了ORM框架 V全拼为View,与MVC中的C功能相同,接收HttpReq ...
- ElasticSearch : 基础简介
1.安装 我用的docker安装,这个用起来比较方便,我是在腾讯云部署的docker,具体的过两天总结一下 安装: docker pull elasticsearch 运行: docker run - ...
- Android中活动的最佳实践(如何很快的看懂别人的代码activity)
这种方法主要在你拿到别人的代码时候很多activity一时半会儿看不懂,用了这个方法以后就可以边实践操作就能够知道具体哪个activity是干什么用的 1.新建一个BaseActivity的类,让他继 ...
- vue中如何动态添加readonly属性
动态绑定input的readonly属性 1 <inpu :readonly="status ? false : 'readonly'"> status 为 false ...
- Thingsboard Gateway开发环境
源码下载地址:https://github.com/thingsboard/thingsboard-gateway 国内大神源码地址:https://github.com/guodaxia103/th ...
- 免费s账号网站
下面网址按排序顺序优先使用,数字越小优先级越高 1,https://io.freess.today/ 2,https://free-ss.site/ 3,https://ss.freess.org/ ...
- 查看mysql事务的隔离级别
1.选择数据库,查看当前事务隔离界别 select @@tx_isolation; 2.开启事务,回滚事务 3.事务级别中脏读,幻读 4.MySQL事务autocommit设置,每次sql必须用com ...
- Composer 安装 Jira API 库
环境要求: PHP >= 5.5.9 php JsonMapper phpdotenv 安装 下载安装 Composer curl -sS https://getcomposer.org/ins ...
- ISO/IEC 9899:2011 条款6.5.17——逗号操作符
6.5.17 逗号操作符 语法 1.expression: assignment-expression expression , assignment-expression 语义 2.一个 ...