当前位置: 首页 > news >正文

104二叉树的最大深度

def maxDepth(self, root):return self.getdepth(root)
  • 直接调用getdepth方法计算根节点的最大深度

getdepth方法

def getdepth(self, node):if not node:  # 基准情况:空节点深度为0return 0leftheight = self.getdepth(node.left)  # 递归计算左子树深度rightheight = self.getdepth(node.right)  # 递归计算右子树深度height = 1 + max(leftheight, rightheight)  # 当前节点深度 = 1 + 左右子树最大深度
return height

具体例子

考虑以下二叉树:


递归计算过程

  1. 从根节点3开始:
    • 计算左子树9的深度:
      • 9是叶子节点,left=right=0 → 深度=1
    • 计算右子树20的深度:
      • 20的左子树15:
        • 15是叶子节点 → 深度=1
      • 20的右子树7:
        • 7是叶子节点 → 深度=1
      • 20的深度 = 1 + max(1,1) = 2
    • 根节点3的深度 = 1 + max(1,2) = 3

递归调用栈图示


再举一个例子:

我们调用 maxDepth(1),它会执行 getdepth(1),然后递归计算每个子树的深度。

步骤 1:计算 getdepth(1)

  • 1 不是 None,继续执行。
  • 计算 getdepth(1.left) → getdepth(2)(左子树)
  • 计算 getdepth(1.right) → getdepth(3)(右子树)
  • 最终返回 1 + max(左子树深度, 右子树深度)

步骤 2:计算 getdepth(2)

  • 2 不是 None,继续执行。
  • 计算 getdepth(2.left) → getdepth(4)(左子树)
  • 计算 getdepth(2.right) → getdepth(5)(右子树)
  • 返回 1 + max(左子树深度, 右子树深度)

步骤 3:计算 getdepth(4)

  • 4 不是 None,继续执行。
  • 4.left 是 None → getdepth(None) 返回 0
  • 4.right 是 None → getdepth(None) 返回 0
  • 返回 1 + max(0, 0) = 14 是叶子节点)

步骤 4:计算 getdepth(5)

  • 5 不是 None,继续执行。
  • 5.left 是 6 → getdepth(6)(左子树)
  • 5.right 是 None → getdepth(None) 返回 0
  • 返回 1 + max(左子树深度, 0)

步骤 5:计算 getdepth(6)

  • 6 不是 None,继续执行。
  • 6.left 是 None → getdepth(None) 返回 0
  • 6.right 是 None → getdepth(None) 返回 0
  • 返回 1 + max(0, 0) = 16 是叶子节点)

步骤 6:回代计算 getdepth(5)

  • getdepth(5) 的 左子树深度 = getdepth(6) = 1
  • getdepth(5) 的 右子树深度 = 0
  • 返回 1 + max(1, 0) = 2

步骤 7:回代计算 getdepth(2)

  • getdepth(2) 的 左子树深度 = getdepth(4) = 1
  • getdepth(2) 的 右子树深度 = getdepth(5) = 2
  • 返回 1 + max(1, 2) = 3

步骤 8:计算 getdepth(3)

  • 3 不是 None,继续执行。
  • 3.left 是 None → getdepth(None) 返回 0
  • 3.right 是 None → getdepth(None) 返回 0
  • 返回 1 + max(0, 0) = 13 是叶子节点)

步骤 9:回代计算 getdepth(1)

  • getdepth(1) 的 左子树深度 = getdepth(2) = 3
  • getdepth(1) 的 右子树深度 = getdepth(3) = 1
  • 返回 1 + max(3, 1) = 4

最终结果

  • maxDepth(1) 返回 4,即该二叉树的最大深度。

递归调用栈总结

递归调用左子树深度右子树深度返回结果
getdepth(6)001 + max(0, 0) = 1
getdepth(5)getdepth(6)=101 + max(1, 0) = 2
getdepth(4)001 + max(0, 0) = 1
getdepth(2)getdepth(4)=1getdepth(5)=21 + max(1, 2) = 3
getdepth(3)001 + max(0, 0) = 1
getdepth(1)getdepth(2)=3getdepth(3)=11 + max(3, 1) = 4

关键点

  1. 递归终止条件:当 node 是 None 时,返回 0(表示空树的深度为 0)。
  2. 递归分解问题
    • 计算 左子树的深度
    • 计算 右子树的深度
    • 当前节点的深度 = 1 + max(左子树深度, 右子树深度)
  3. 最终结果:根节点的深度就是整棵树的最大深度。
http://www.xdnf.cn/news/308395.html

相关文章:

  • 标签语句分析
  • 第11次:用户注册(简要版)
  • 【大模型面试】大模型(LLMs)高频面题全面整理(★2025年5月最新版★)
  • 13前端项目----购物车修改
  • 结合Hutool 突增突降检测的算法
  • Linuxweb服务的部署及优化
  • 网站主机控制面板深度解析:cPanel、Plesk 及其他主流选择
  • AIDC智算中心建设:存储核心技术解析
  • suna直接从agent启动时,死循环问题
  • “FATAL ERROR: Reached heap limit Allocation failed” NodeJS 错误解决方案
  • URP - 深度图
  • 多模态大语言模型arxiv论文略读(六十一)
  • 码蹄集——直线切平面、圆切平面
  • postgresql 创建、移出数据保留策略
  • WiFi那些事儿(八)——802.11n
  • 基于Anaconda的Pycharm环境配置
  • 【IP101】图像处理进阶:从直方图均衡化到伽马变换,全面掌握图像增强技术
  • 游戏的TypeScript(6)TypeScript的元编程
  • 高级java每日一道面试题-2025年5月03日-基础篇[反射篇-编码]-使用反射创建`java.util.Date`对象,并调用其无参构造方法。
  • 【PPT制作利器】DeepSeek + Kimi生成一个初始的PPT文件
  • 安全不止一层:多因素认证的实现与管理指南
  • 荣耀A8互动娱乐组件部署实录(第1部分:服务端环境搭建)
  • 学习人工智能开发的详细指南
  • Kubernetes弹性伸缩:让应用自动应对流量洪峰与低谷
  • 如何在 Vue3 中更好地使用 Typescript
  • POI创建Excel文件
  • ubantu安装CUDA
  • uniapp开发11-v-for动态渲染list列表数据
  • 26届秋招收割offer指南
  • 基于SpringBoot网上书店的设计与实现