当前位置:网站首页>力扣解法汇总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
- 详细了解Redis的八种数据类型及应用场景分析
- The core concept of JMM: happens before principle
- Research and investment strategy report on China's nano silver wire conductive film industry (2022 Edition)
- Pousser l'information au format markdown vers le robot nail
- Source code reading | the process of reading text format STL by openmesh
- Based on the codeless platform, users deeply participated in the construction, and digital data + Nanjing Fiberglass Institute jointly built a national smart laboratory solution
- Canvas to add watermark to pictures
- laravel用户授权
- Beijiafu (p+f) R2000 modified radar IP
猜你喜欢

2022安全员-B证考试题库及答案

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

Tech Talk 活动回顾|云原生 DevOps 的 Kubernetes 技巧

推送Markdown格式信息到钉钉机器人

ThreadLocal local thread

07_SpingBoot 实现 RESTful 风格

对抗训练理论分析:自适应步长快速对抗训练

Non single file component

2022-06-16 work record --js- judge the number of digits in string type digits + judge the number of digits in numeric type digits + limit the text length (display n words at most, exceeding...)

结构体的内存对齐
随机推荐
[laravel series 7.9] test
Dynamic memory management (1)
Research Report on terahertz imaging system industry - market status analysis and development prospect forecast
花房集团二次IPO:成于花椒,困于花椒
Annotation
2022 simulated 100 questions and simulated examination of high-altitude installation, maintenance and demolition
Database transaction Transanction
结构体的内存对齐
「ARM 架构」是一种怎样的处理器架构?
How to submit the shopee opening and settlement flow?
Attention, postgraduate candidates! They are the easiest scams to get caught during the preparation period?!
Talk about GC mechanism often asked in interview
The large-scale market of graduate dormitory! Here comes the enviable graduate dormitory!
cat写多行内容到文件
Learning bit segment (1)
MySQL夺命10问,你能坚持到第几问?
Some updates about a hand slider (6-18, JS reverse)
【Mongodb】READ_ME_TO_RECOVER_YOUR_DATA,数据库被恶意删除
C#学习两年的增删改查和C#导入导出(去重)案例
是否需要提高代码阅读能力?这有技巧