当前位置:网站首页>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
复杂度分析
时间复杂度:
空间复杂度:
边栏推荐
- 5次折戟IPO,互联网家装这条路,没土巴兔想的那么简单
- 软件测试基础笔记
- Leetcode 23.合并K个升序链表 链表归并合并
- 快速搭建SSM框架
- PyTorch入门:(二)Tensorboard的使用
- What are the three main aspects of digital factory construction?
- CAD进阶练习(二)
- 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
- PyTorch入门:(一)数据加载
- 分布式文件系统fastDFS
猜你喜欢
golang流程控制:if分支、switch分支和fallthrough switch穿透
疫情期间闲来无事,我自制了一个按钮展示框特效来展示我的博客
计算机网络面试常问知识
能力一般,却可以大厂随便横跳?强在哪里?
快速搭建SSM框架
从 VLAN 到 IPVLAN: 聊聊虚拟网络设备及其在云原生中的应用
Geometric g6 will carry harmonyos system, a comprehensive upgrade competitiveness of products
Redis之SDS数据结构
工程 (六) ——PointNet点云分类
Build DG will increase the amount of lead to archive log problem
随机推荐
阿里云数据库PolarDB开源人才培养计划发布!万元好礼等你来拿!
分布式文件系统fastDFS
Learn about layered architecture & SOA architecture together
Research on ORACLE subqueries that lead to inability to push predicates
【761. Special binary sequence】
Is it safe to open an account with Qiniu Business School?Is it reliable to open an account?
uniapp parent component uses prop to pass asynchronous data to child components
黑猫带你学Makefile第8篇:uboot/kernel中的makefile基本语法与流程
腾讯云原生成本优化平台FinOps Crane荣获国家级大奖!
nyoj714 Card Trick (The 6th Henan Province Programming Contest)
MogDB study notes - starting from 0
能力一般,却可以大厂随便横跳?强在哪里?
shell的各种三角形
证券开户选哪个券商平台比较好,哪个更安全
即将开幕!阿里云飞天技术峰会邀您一同探秘云原生最佳实践
Redhat 7 Maria DB installation and configuration
FastDFS distributed file system
Flutter Chart
Implement the entire process of Mock API with tools
互联网技术从业者怎么解决系统高并发?