【20250216】二叉树:515.在每个树中找最大值

class Solution:

def largestValues(self, root: TreeNode) -> List[int]:

if not root:

return []

result = []

queue = deque([root])

while queue:

size = len(queue)

max_val = float('-inf')

for i in range(size):

cur = queue.popleft()

max_val = max(max_val, cur.val)

if cur.left:

queue.append(cur.left)

if cur.right:

queue.append(cur.right)

result.append(max_val)

return result

class Solution:

def largestValues(self,root:TreeNode)->List[int]:

if not root:

return []

res=[]

queue=deque([root])

while queue:

level_max=float('-inf')

size=len(queue)

for i in range(size):

cur=queue.popleft()

level_max=max(level_max,cur.val)

if cur.left:

queue.append(cur.left)

if cur.right:

queue.append(cur.right)

res.append(level_max)

return res

相关推荐
liuhaoran___6 分钟前
解释区块链技术的应用场景和优势
python
独好紫罗兰7 分钟前
洛谷题单2-P5712 【深基3.例4】Apples-python-流程图重构
开发语言·python·算法
uhakadotcom19 分钟前
NVIDIA Resiliency Extension(NVRx)简介:提高PyTorch训练的容错性
算法·面试·github
东方佑22 分钟前
深度解析Python-PPTX库:逐层解析PPT内容与实战技巧
开发语言·python·powerpoint
Python大数据分析@31 分钟前
python 常用的6个爬虫第三方库
爬虫·python·php
梭七y36 分钟前
【力扣hot100题】(020)搜索二维矩阵Ⅱ
算法·leetcode·职场和发展
一顿操作猛如虎,啥也不是!39 分钟前
JAVA-Spring Boot多线程
开发语言·python
v维焓1 小时前
C++(思维导图更新)
开发语言·c++·算法
斯内科1 小时前
Python入门(7):Python序列结构-字典
python·字典·dictionary
云徒川1 小时前
【设计模式】过滤器模式
windows·python·设计模式