当前位置:网站首页>LeetCode #104.二叉树的最大深度
LeetCode #104.二叉树的最大深度
2022-08-08 19:11:00 【张楚明ZCM】
题目截图
方法一:深度优先搜索(递归)
将二叉树不断分解,直到左子树和右子树的最大深度即可知道二叉树的最大深度。
每个字数又可以不断分解成新的字数,直到没有结点,每一次分解,加一层。
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root :
return 0
return max(self.maxDepth(root.left),self.maxDepth(root.right))+1
复杂度分析
时间复杂度:其中
为二叉树节点的个数。每个节点在递归中只被遍历一次。
空间复杂度:其中
表示二叉树的高度。
完整测试代码
from typing import Optional
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
return max(self.maxDepth(root.left),self.maxDepth(root.right))+1
class main:
a = Solution()
node1 = TreeNode(3)
node2 = TreeNode(9)
node3 = TreeNode(20)
node4 = TreeNode(15)
node5 = TreeNode(7)
node1.left = node2
node1.right = node3
node3.left = node4
node3.right = node5
b = a.maxDepth(node1)
print(b)
if __name__ == '__main__':
main()
方法二:广度优先搜索
利用队列保存每一层的所有结点,把队列里的所有结点出队列,然后把出队列的结点的子节点加入队列。
如例题中的二叉树
刚开始,深度为0。
先加入第一层的3,然后第一层的3出队列,第一层所有结点出队列,深度+1。
加入第二层的9和20,然后9出队列,由于9没有子节点,不添加新的结点进入队列。
再让20出队列,此时第二层所有结点已经出队列,深度+1。
加入第三层的15和7。
再分别让15和7出队列,由于没有子节点,不添加任何新节点,此时所有结点出队列,队列为空,深度+1。
最终返回深度,深度为3。
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if root == None:
return 0
queue = [root]
depth = 0
while queue:
n = len(queue)
for i in range(n):
node = queue.pop(0)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
depth += 1
return depth
复杂度分析
时间复杂度:
空间复杂度:
边栏推荐
- Wps文档云同步如何开启?Wps打开文档云同步的方法
- 从 VLAN 到 IPVLAN: 聊聊虚拟网络设备及其在云原生中的应用
- PyTorch入门:(五)模型的搭建
- laravel run scheduler command on weekdays (except holidays)
- synApps -- Autosave
- [极客大挑战 2019]BuyFlag&&[HCTF 2018]admin
- Lecture 4: Database Definition Language of DDL Type of SQL Statements
- 数字化工厂建设的内容主要有哪三个方面
- What are the three main aspects of digital factory construction?
- 使用dedecms自带采集功能的文字过滤与替换
猜你喜欢
Dandelion R300A 4G router, remote monitoring PLC tutorial
Research on ORACLE subqueries that lead to inability to push predicates
Implement the entire process of Mock API with tools
Oracle - table
Wps文档云同步如何开启?Wps打开文档云同步的方法
Vue program of web cache problem after packaging
Geometric g6 will carry harmonyos system, a comprehensive upgrade competitiveness of products
软考中级网络工程师全面学习笔记第2版(5万字)+配套视频及课件
[MRCTF2020]你传你码呢
SSM project integration, integrated case
随机推荐
The history of cartoon rendering
C language elementary - structure
Codeforces Round #713 (Div. 3) E(思维)
JVM调优-JVM调优实践一
openEuler 资源利用率提升之道02:典型应用下的效果
shell的各种三角形
黑猫带你学Makefile第1篇:什么是Makefile
文档管理系统对于企业来说有哪些作用?
培训预告 | 企业应用现代化实用教程——DevOps方法论及最佳实践篇 8月11日上线
商品期货需要多少钱开户。有资金门槛吗?期货开户在哪开安全?
Will ODPS spark on Dataworks process data more efficiently than directly using ODPS SQL?
企业进行知识共享的好处有哪些?
Codeforces Round #707 (Div. 2) C(抽屉原理)
数字化工厂建设的内容主要有哪三个方面
Goose Factory Robot Dog Fancy Crossing 10m Plum Blossom Pile: Front Flip, Single Pile Jump, Get Up and Bow... No stumble in the whole process
微信小程序第一集
工程 (六) ——PointNet点云分类
01. Preface
Generate captchas tools
Is there any function in MAXCOMPUTE SQL to judge whether the string is a number?