当前位置:网站首页>Force deduction solution summary 515- find the maximum value in each tree row
Force deduction solution summary 515- find the maximum value in each tree row
2022-06-24 23:11:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link : Power button
describe :
Given the root node of a binary tree root , Please find the maximum value of each layer in the binary tree .
Example 1:
Input : root = [1,3,2,5,3,null,9]
Output : [1,3,9]
Example 2:
Input : root = [1,2,3]
Output : [1,3]
Tips :
The range of the number of nodes in a binary tree is [0,104]
-231 <= Node.val <= 231 - 1
source : Power button (LeetCode)
link :https://leetcode.cn/problems/find-largest-value-in-each-tree-row
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * use list Record the maximum value of each level . Yes TreeNode Do recursive traversal , Incoming value is hierarchy / Nodes and list.
Code :
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);
}
}
}边栏推荐
- 【Laravel系列7.9】测试
- What kind of processor architecture is ARM architecture?
- Environment configuration | vs2017 configuring openmesh source code and environment
- 15 lines of code using mathematical formulas in wangeditor V5
- laravel 创建 service层
- Research and investment strategy report on China's nano silver wire conductive film industry (2022 Edition)
- 记录一下MySql update会锁定哪些范围的数据
- vulnhub Vegeta: 1
- Cat write multiline content to file
- laravel 定时任务
猜你喜欢
随机推荐
Introduction to machine learning compilation course learning notes lesson 1 overview of machine learning compilation
Uip1.0 active sending problem understanding
docker-mysql8-主从
EMI的主要原因-工模电流
2022年高处安装、维护、拆除考试模拟100题及模拟考试
gocolly-手册
[untitled]
The large-scale market of graduate dormitory! Here comes the enviable graduate dormitory!
Selection (025) - what is the output of the following code?
【文本数据挖掘】中文命名实体识别:HMM模型+BiLSTM_CRF模型(Pytorch)【调研与实验分析】
Uncover the secrets of Huawei cloud enterprise redis issue 16: acid'true' transactions beyond open source redis
Getting started with the go Cobra command line tool
Cat write multiline content to file
「ARM 架构」是一种怎样的处理器架构?
Are you afraid of being asked MySQL related questions during the interview? This 30000 word essence summary + 100 interview questions, and it's enough to hang the interviewer
01_SpingBoot 框架入门
剑指 Offer 13. 机器人的运动范围
docker安装redis-简单而无坑
2022 safety officer-b certificate examination question bank and answers
Development specification - parameter verification exception, exception return prompt section








