当前位置:网站首页>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);
}
}
}边栏推荐
- 01_SpingBoot 框架入门
- A big factory interview must ask: how to solve the problem of TCP reliable transmission? 8 pictures for you to learn in detail
- 【nvm】
- Research Report on terahertz imaging system industry - market status analysis and development prospect forecast
- laravel 消息队列
- Research Report on market evaluation and investment direction of Chinese dermatology drugs (2022 Edition)
- Construction equipment [6]
- What kind of processor architecture is ARM architecture?
- Research and investment strategy report on China's building steel structure anticorrosive coating industry (2022 Edition)
- 03_SpingBoot 核心配置文件
猜你喜欢

Servlet

Parental delegation mechanism

Recommended movies: Northeast tiger, licorice pizza
Mycms we media CMS V3.0, resource push optimization, new free template

07_ Springboot for restful style

vulnhub DC: 2

Getting started with the go Cobra command line tool

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

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!

Analyze the implementation process of oauth2 distributed authentication and authorization based on the source code
随机推荐
Selection (027) - what is the output of the following code?
并发之共享模型管程
China smallpox vaccine market trend report, technical innovation and market forecast
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
shopee开店入驻流水如何提交?
The large-scale market of graduate dormitory! Here comes the enviable graduate dormitory!
Building Survey [2]
推送Markdown格式信息到釘釘機器人
2022 safety officer-b certificate examination question bank and answers
Talk about GC mechanism often asked in interview
Research Report on market evaluation and investment direction of Chinese dermatology drugs (2022 Edition)
[ROS play with turtle turtle]
How should we measure agile R & D projects?
China solar thermal market trend report, technical dynamic innovation and market forecast
Daily practice (22): maximum sum of continuous subarrays
2022安全员-B证考试题库及答案
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
01_SpingBoot 框架入门
High level application of SQL statements in MySQL database (I)
Financial management [5]