当前位置:网站首页>小黑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

边栏推荐
- MySQL Series 1: Account Management and Engine
- typescript17 - function optional parameters
- MySQL notes under
- IOT cross-platform component design scheme
- Solution: Parameter 0 of method ribbonServerList in com.alibaba.cloud.nacos.ribbon.NacosRibbonClientConfigu
- 【愚公系列】2022年07月 Go教学课程 019-循环结构之for
- MySql数据恢复方法个人总结
- 华为“天才少年”稚晖君又出新作,从零开始造“客制化”智能键盘
- 小程序-全局数据共享
- typescript13-类型别名
猜你喜欢

Error occurred while trying to proxy request The project suddenly can't get up

Yolov7实战,实现网页端的实时目标检测

【Multithreading】

typescript15- (specify both parameter and return value types)
![[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

297. 二叉树的序列化与反序列化

typescript10-commonly used basic types

Meeting OA project pending meeting, all meeting functions

Jmeter参数传递方式(token传递,接口关联等)

ShardingSphere's public table combat (7)
随机推荐
查看zabbix-release-5.0-1.el8.noarch.rpm包内容
权限管理怎么做的?
BOM系列之Navigator对象
297. 二叉树的序列化与反序列化
Sping.事务的传播特性
Filter (Filter)
Shell编程之条件语句
typescript15- (specify both parameter and return value types)
XSS related knowledge
Huawei's "genius boy" Zhihui Jun has made a new work, creating a "customized" smart keyboard from scratch
DOM系列之动画函数封装
typescript18-对象类型
typescript11 - data types
MySQL的触发器
go mode tidy出现报错go warning “all“ matched no packages
分布式系统的一致性与共识(1)-综述
Bypass of xss
Basic usage of async functions and await expressions in ES6
系统设计.短链系统设计
Go study notes (84) - Go project directory structure