当前位置:网站首页>小黑leetcode之旅:104. 二叉树的最大深度
小黑leetcode之旅:104. 二叉树的最大深度
2022-07-31 00:52:00 【小黑无敌】
小黑解法一:深度优先
# 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 maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
def getDepth(node):
if node:
return max(getDepth(node.left),getDepth(node.right)) + 1
else:
return 0
return getDepth(root)

小黑解法二:宽度优先
# 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 maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
q = collections.deque([root])
depth = 0
while q:
# 队列中结点的个数
n = len(q)
for i in range(n):
node = q.popleft()
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
depth += 1
return depth

边栏推荐
- 深度学习可以求解特定函数的参数么?
- Jetpack Compose learning (8) - State and remeber
- [C language course design] C language campus card management system
- typescript16-void
- 【Demo】ABAP Base64加解密测试
- Responsive layout vs px/em/rem
- MySQL系列一:账号管理与引擎
- 网站频繁出现mysql等数据库连接失败等信息解决办法
- API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试
- DOM系列之 client 系列
猜你喜欢

How to Add a Navigation Menu on Your WordPress Site

In Google Cloud API gateway APISIX T2A and T2D performance test

分布式.幂等性
![[Tang Yudi Deep Learning-3D Point Cloud Combat Series] Study Notes](/img/52/88ad349eca136048acd0f328d4f33c.png)
[Tang Yudi Deep Learning-3D Point Cloud Combat Series] Study Notes

typescript11 - data types

论文理解:“Designing and training of a dual CNN for image denoising“

一万了解 Gateway 知识点

typescript10-commonly used basic types

【多线程】

The difference between h264 and h265 decoding
随机推荐
【Yugong Series】July 2022 Go Teaching Course 013-Constants, Pointers
MySQL database constraints, table design
Neural Network (ANN)
DOM系列之动画函数封装
MySQL数据库面试题总结(2022最新版)
unity2D横版游戏教程4-物品收集以及物理材质
ShardingSphere's vertical sub-database sub-table actual combat (5)
Responsive layout vs px/em/rem
WEB安全基础 - - -漏洞扫描器
GO GOPROXY proxy Settings
ShardingSphere's public table combat (7)
Error in go mode tidy go warning “all” matched no packages
typescript18-对象类型
过滤器(Filter)
The difference between h264 and h265 decoding
【愚公系列】2022年07月 Go教学课程 019-循环结构之for
MySQL Series 1: Account Management and Engine
The client series of the DOM series
MySQL database advanced articles
Jetpack Compose learning (8) - State and remeber