当前位置:网站首页>Leetcode daily question - 515 Find the maximum value in each tree row
Leetcode daily question - 515 Find the maximum value in each tree row
2022-06-28 20:36:00 【Did HYK write the algorithm today】
List of articles
subject
Given the root node of a binary tree root , Please find the maximum value of each layer in the binary tree .
Example
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
Ideas
BFS, Simulating sequence traversal using queues , List the maximum value of each layer
Answer key
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
from collections import deque
class Solution:
def largestValues(self, root: Optional[TreeNode]) -> List[int]:
# Queue and result list
temp,res = deque(), []
if root is None:
return res
else:
temp.append(root)
while temp:
n, max_ = len(temp), -float('inf')
# Update the maximum value of each layer
for i in range(n):
index = temp.popleft()
max_ = max(max_, index.val)
if index.left is not None:
temp.append(index.left)
if index.right is not None:
temp.append(index.right)
res.append(max_)
return res
边栏推荐
- 怎么理解云原生数据库的快速迭代?
- [go language questions] go from 0 to entry 5: comprehensive review of map, conditional sentences and circular sentences
- Fix the simulator that cannot be selected by flutter once
- Résumé de la stabilité
- Troubleshooting of pyinstaller failed to pack pikepdf
- How to add logs to debug anr problems
- List of domestic database directory
- odoo15 Module operations are not possible at this time, please try again later or contact your syste
- [learning notes] factor analysis
- No module named ‘PyEMD‘ ; Use plt figure()TypeError: ‘module‘ object is not callable
猜你喜欢

Ref attribute, props configuration, mixin mixing, plug-in, scoped style

我也差点“跑路”

Racher add / delete node
How to recover after Oracle delete accidentally deletes table data

如何使用 DataAnt 监控 Apache APISIX

Analysis of variance

Leetcode 36. 有效的数独(可以,一次过)

Leetcode 36. Effective Sudoku (yes, once)

数据资产为王,如何解析企业数字化转型与数据资产管理的关系?

openGauss内核分析之查询重写
随机推荐
2022 tea master (intermediate) examination simulated 100 questions and simulated examination
Troubleshooting of pyinstaller failed to pack pikepdf
3. 整合 Listener
03.hello_rust
学习太极创客 — MQTT 第二章(八)ESP8266 MQTT 用户密码认证
API gateway Apache APIs IX helps the evolution of snowball dual active architecture
How to open an account in great wisdom? Is it safe
Input and output character data
Alibaba cloud MSE full link grayscale solution practice based on Apache apisik
Flatten of cnn-lstm
Are you still paying for your thesis? Come and join me
实型数运算
odoo15 Module operations are not possible at this time, please try again later or contact your syste
1. 整合 Servlet
LeetCode每日一题——710. 黑名单中的随机数
Resilience4j retry source code analysis and retry index collection
Pipeline | and redirection >
Day88. qiniu cloud: upload house source pictures and user avatars
Lecture 30 linear algebra Lecture 4 linear equations
Figure neural network can also be used as CV backbone model. Huawei Noah Vig architecture is comparable to CNN and transformer
