当前位置:网站首页>leetcode:515. 在每个树行中找最大值【无脑bfs】
leetcode:515. 在每个树行中找最大值【无脑bfs】
2022-06-24 19:30:00 【白速龙王的回眸】

分析
无脑bfs
ac code
# 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
class Solution:
def largestValues(self, root: Optional[TreeNode]) -> List[int]:
if not root:
return []
ans = []
q = [root]
while q:
new_q = []
maxn = -inf
for node in q:
maxn = max(maxn, node.val)
if node.left: new_q.append(node.left)
if node.right: new_q.append(node.right)
ans.append(maxn)
q = new_q
return ans
总结
无脑bfs
边栏推荐
- 应用实践 | 海量数据,秒级分析!Flink+Doris 构建实时数仓方案
- leetcode1863_ 2021-10-14
- Transport layer UDP & TCP
- Multiplexer select
- 基于 KubeSphere 的分级管理实践
- C language - keyword 1
- Reduce the pip to the specified version (upgrade the PIP through CMP and reduce it to the original version)
- [200 opencv routines] 209 Color image segmentation in HSV color space
- 心楼:华为运动健康的七年筑造之旅
- Reduce the pip to the specified version (upgrade the PIP through pycharm, and then reduce it to the original version)
猜你喜欢

LeetCode-513. Find the value in the lower left corner of the tree

Xinlou: Huawei's seven-year building journey of sports health

Detailed installation and use of performance test tool wrk

Several schemes of traffic exposure in kubernetes cluster

Kubernetes 集群中流量暴露的几种方案

The collection of zero code enterprise application cases in various industries was officially released

SAP接口debug设置外部断点

想当测试Leader,这6项技能你会吗?

Multithreaded finalization

Redis+caffeine two-level cache enables smooth access speed
随机推荐
栈的两种实现方式
【无标题】
机器学习:线性回归
Minimum spanning tree based on Kruskal
Installing Oracle without graphical interface in virtual machine centos7 (nanny level installation)
【论】Deep learning in the COVID-19 epidemic: A deep model for urban traffic revitalization index
【吴恩达笔记】机器学习基础
如何化解35岁危机?华为云数据库首席架构师20年技术经验分享
[notes of Wu Enda] multivariable linear regression
是真干不过00后,给我卷的崩溃,想离职了...
suspense组件和异步组件
linq查询集合类入门 案例武林高手类
[camera Foundation (II)] camera driving principle and Development & v4l2 subsystem driving architecture
leetcode_1470_2021.10.12
Excel layout
如何做到全彩户外LED显示屏节能环保
Redis+Caffeine两级缓存,让访问速度纵享丝滑
【吴恩达笔记】多变量线性回归
拖动拖动拖动
Summary of papers on traveling salesman problem (TSP)