【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

相关推荐
小雨下雨的雨10 小时前
井字棋AI机器人实现详解 - Minimax算法实战-鸿蒙PC Electron框架完成
前端·人工智能·算法·华为·electron·鸿蒙
xieliyu.12 小时前
Java算法精讲:双指针(三)
java·开发语言·算法
love530love12 小时前
LiveTalking 数字人项目 Windows 部署完全指南(EPGF 架构)
人工智能·windows·python·架构·livetalking·epgf
遇事不決洛必達12 小时前
【Python基础】GIL 锁是什么及其对爬虫的影响
爬虫·python·线程·进程·gil锁
一条小锦吕*12 小时前
基于Spring Boot + 数据可视化 + 协同过滤算法的推荐系统设计与实现(源码+论文+部署全讲解)
spring boot·算法·信息可视化
CryptoPP13 小时前
快速对接东京证券交易所API数据:实战指南与代码示例
开发语言·人工智能·windows·python·信息可视化·区块链
探物 AI14 小时前
把 MambaOut 塞进 YOLOv11:会有什么样的反应
python·yolo·计算机视觉
如竟没有火炬14 小时前
最大矩阵——单调栈
数据结构·python·线性代数·算法·leetcode·矩阵
阳区欠14 小时前
【LangChain】LLM基础介绍
开发语言·python·langchain
Cosolar14 小时前
保姆级 CrewAI 教程:从零构建多智能体协作系统
人工智能·python·架构