当前位置:网站首页>力扣解法汇总515-在每个树行中找最大值
力扣解法汇总515-在每个树行中找最大值
2022-06-24 19:41:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
给定一棵二叉树的根节点 root ,请找出该二叉树中每一层的最大值。
示例1:
输入: root = [1,3,2,5,3,null,9]
输出: [1,3,9]
示例2:
输入: root = [1,2,3]
输出: [1,3]
提示:
二叉树的节点个数的范围是 [0,104]
-231 <= Node.val <= 231 - 1
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-largest-value-in-each-tree-row
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 用list记录每一次层级最大的值。对TreeNode进行递归遍历,传入值为层级/节点和list。
代码:
public class Solution515 {
public List<Integer> largestValues(TreeNode root) {
List<Integer> list = new ArrayList<>();
if (root == null) {
return list;
}
search(0, root, list);
return list;
}
private void search(int level, TreeNode node, List<Integer> list) {
if (list.size() <= level) {
list.add(node.val);
} else {
list.set(level, Math.max(list.get(level), node.val));
}
if (node.left != null) {
search(level + 1, node.left, list);
}
if (node.right != null) {
search(level + 1, node.right, list);
}
}
}边栏推荐
- 2022 safety officer-a certificate examination questions and answers
- Wechat side: what is consistent hash? In what scenario? What problems have been solved?
- 研究生宿舍大盘点!令人羡慕的研究生宿舍来了!
- Development specification - parameter verification exception, exception return prompt section
- Common sense of resolution
- 【Laravel系列7.9】测试
- A big factory interview must ask: how to solve the problem of TCP reliable transmission? 8 pictures for you to learn in detail
- 机器学习编译入门课程学习笔记第一讲 机器学习编译概述
- Talk about GC mechanism often asked in interview
- MySQL + JSON = King fried!!
猜你喜欢

The large-scale market of graduate dormitory! Here comes the enviable graduate dormitory!

03_SpingBoot 核心配置文件
Based on the codeless platform, users deeply participated in the construction, and digital data + Nanjing Fiberglass Institute jointly built a national smart laboratory solution

Spark 离线开发框架设计与实现

Web security XSS foundation 06

See how sparksql supports enterprise data warehouse

vulnhub Vegeta: 1

面试害怕被问MySQL相关问题 ?这份三万字精华总结 + 面试100 问,吊打面试官完全够了

Environment configuration | vs2017 configuring openmesh source code and environment

Development specification - parameter verification exception, exception return prompt section
随机推荐
Sword finger offer 13 Range of motion of robot
vulnhub Vegeta: 1
Introduction to machine learning compilation course learning notes lesson 1 overview of machine learning compilation
Learning bit segment (1)
EPICS記錄參考3 -- 所有記錄都有的字段
Solve the problem of port occupation
Design and implementation of spark offline development framework
China Sky Lantern market trend report, technical dynamic innovation and market forecast
Common sense of resolution
The large-scale market of graduate dormitory! Here comes the enviable graduate dormitory!
Database transaction Transanction
The difference between get and post
Learn more about the practical application of sentinel
别再乱用了,这才是 @Validated 和 @Valid 的真正区别!!!
环境配置 | VS2017配置OpenMesh源码和环境
双亲委派机制
Web security XSS foundation 06
Recommended movies: Northeast tiger, licorice pizza
vulnhub DC: 2
Problem solving - nested lists