当前位置:网站首页>力扣解法汇总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);
}
}
}边栏推荐
- Servlet
- laravel学习笔记
- The large-scale market of graduate dormitory! Here comes the enviable graduate dormitory!
- Research Report on terahertz imaging system industry - market status analysis and development prospect forecast
- 结合源码剖析Oauth2分布式认证与授权的实现流程
- MySQL + JSON = King fried!!
- China smallpox vaccine market trend report, technical innovation and market forecast
- Solution to the login error of tangdou people
- 2022年高压电工考试模拟100题及在线模拟考试
- China Sky Lantern market trend report, technical dynamic innovation and market forecast
猜你喜欢

【文本数据挖掘】中文命名实体识别:HMM模型+BiLSTM_CRF模型(Pytorch)【调研与实验分析】

23研考生注意啦!备考期间最容易中招的骗局,居然是它们?!

The extra points and sharp tools are worthy of the trust | know that Chuangyu won the letter of thanks from the defense side of the attack and defense drill!

Pousser l'information au format markdown vers le robot nail

Beijiafu (p+f) R2000 modified radar IP

2022年安全员-A证考题及答案

EPICS记录参考2--EPICS过程数据库概念

结合源码剖析Oauth2分布式认证与授权的实现流程

Getting started with the go Cobra command line tool

Leetcode: push domino (domino simulation)
随机推荐
非单文件组件
The difference between get and post
Second IPO of Huafang group: grown up in Zanthoxylum bungeanum, trapped in Zanthoxylum bungeanum
Simulated 100 questions and online simulated examination of high voltage electrician examination in 2022
docker安装redis-简单而无坑
Research and investment strategy report on China's nano silver wire conductive film industry (2022 Edition)
Tech Talk 活动回顾|云原生 DevOps 的 Kubernetes 技巧
Research Report on market supply and demand and strategy of China's solar charging controller industry
vulnhub DC: 2
详细了解Redis的八种数据类型及应用场景分析
动态菜单,自动对齐
AttacKG: Constructing Technique Knowledge Graph from Cyber Threat Intelligence Reports代码复现
[Wuhan University] information sharing of the first and second postgraduate entrance examinations
JD 618 conference tablet ranking list announced that the new dark horse brand staff will compete for the top three, learning from Huawei, the leader of domestic products
推送Markdown格式信息到钉钉机器人
Leetcode algorithm refers to offer II 027 Palindrome linked list
O (n) complexity hand tear sorting interview questions | an article will help you understand counting sorting
Solve the problem of port occupation
How to submit the shopee opening and settlement flow?
Do you need to improve your code reading ability? It's a trick