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

class Solution:

def largestValues(self, root: TreeNode) -> Listint:

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)->Listint:

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

相关推荐
Dmz丶9 分钟前
前端转 AI 100 天 Day 55:RAG 项目实战(四)——前端完善,做一个能用的产品
python
geovindu1 小时前
python: KNeighborsClassifier K
开发语言·后端·python·算法
mattleeee1 小时前
Python脚本超时重试机制设计:失败立即重试,最多3次
python·超时·重试机制
笨鸟先飞的橘猫1 小时前
SLG 后端实战(一):基础建模与行军模拟
python·游戏程序
让我们一起加油好吗3 小时前
【数据结构】线段树详解
数据结构·c++·算法·线段树·洛谷
Thanks_ks3 小时前
接口限流是怎么实现的:从固定窗口计数讲到滑动窗口日志
redis·python·限流·滑动窗口·漏桶·令牌桶·固定窗口
凌冰_3 小时前
数据结构 - 队列(五)
数据结构·算法·软件工程
choicesages4 小时前
跑通不等于跑对:公开评论抓取里三处不报错的坐标问题
爬虫·python·数据清洗·数据质量·去重
让我们一起加油好吗4 小时前
【数据结构】ST 表
数据结构·c++·算法·动态规划·洛谷·st表
鲸渔5 小时前
【AI Agent系列课程33-FastAPI 基础07】自动文档与接口调试:Swagger UI 和 ReDoc 入门
python·ui·fastapi·ai编程