当前位置:网站首页>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
边栏推荐
- 基于 Apache APISIX 的自动化运维平台
- 不同框架的绘制神经网络结构可视化
- 券商公司开户哪个最靠谱最安全呢
- How to analyze the relationship between enterprise digital transformation and data asset management?
- How to understand the fast iteration of cloud native database?
- APISIX 助力中东社交软件,实现本地化部署
- GlobalSign的泛域名SSL证书
- Are you still paying for your thesis? Come and join me
- Lucene构建索引的原理及源代码分析
- 穩定性總結
猜你喜欢

不同框架的绘制神经网络结构可视化

Alibaba cloud MSE full link grayscale solution practice based on Apache apisik

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

Leetcode 36. Effective Sudoku (yes, once)

Rsync remote synchronization

应用实践 | 10 亿数据秒级关联,货拉拉基于 Apache Doris 的 OLAP 体系演进(附 PPT 下载)

LeetCode每日一题——515. 在每个树行中找最大值

Analysis of variance

with torch. no_ Grad(): reason for using

The principle and source code analysis of Lucene index construction
随机推荐
LeetCode每日一题——522. 最长特殊序列 II
圆球等的相关计算
The principle and source code analysis of Lucene index construction
Apisik helps Middle East social software realize localized deployment
[learning notes] Introduction to principal component analysis
Ref attribute, props configuration, mixin mixing, plug-in, scoped style
[graduation season · advanced technology Er] hard work can only pass, hard work can be excellent!
修复一次flutter 无法选中模拟器
稳定性总结
GlobalSign的泛域名SSL证书
Flatten of cnn-lstm
Troubleshooting of pyinstaller failed to pack pikepdf
理解整个网络模型的构建
嵌入式中 动态阿拉伯语字符串 转换 LCD显示字符串【感谢建国雄心】
Why does next() in iterator need to be forcibly converted?
软件watchdog和ANR触发memory dump讲解
Is the inter-bank certificate of deposit reliable and safe
2022 t elevator repair test question bank simulation test platform operation
怎么理解云原生数据库的快速迭代?
iterator中的next()为什么要强转?