当前位置: 首页 > 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. 最终结果:根节点的深度就是整棵树的最大深度。

相关文章:

  • SpringBoot中使用MCP和通义千问来处理和分析数据
  • UKCC(原OUCC)真题讲解(一)
  • 将文本文件加载到excel文件,让数据看的更清晰
  • 江苏正力新能Verify认知能力测评笔试已通知 | SHL测评题库预测题 | 华东同舟求职讲求职
  • Windows下安装EMQX服务代理和MQTTX客户端服务器
  • 新手SEO基础操作入门精要
  • idea更换jdk版本操作
  • C语言位域与结构体打包技术
  • 【漫话机器学习系列】243.数值下溢(Underflow)
  • ​Spring + Shiro 整合的核心要点及详细实现说明
  • 【QT】QT软件编译生成exe后,需要拷贝依赖库使用方法
  • QT键盘触发按钮
  • Android开发-创建、运行、调试App工程
  • 01_线性表
  • Java中的Classpath 包含哪些目录?
  • linux -shell原理与运用
  • openwrt 使用quilt 打补丁(patch)
  • 【Harbor v2.13.0 详细安装步骤 安装证书启用 HTTPS】
  • WebRTC并非万能:RTMP与RTSP的工程级价值再认识
  • Flutter开发IOS蓝牙APP的大坑
  • 泽连斯基:正在等待俄方确认参加会谈的代表团组成
  • 制造四十余年血腥冲突后,库尔德工人党为何自行解散?
  • 加拿大总理宣布新内阁名单
  • 上海护师邢红获第50届南丁格尔奖,她为何能摘得护理界最高荣誉
  • 巴基斯坦对印度发起网络攻击,致其约70%电网瘫痪
  • OpenAI任命了一位新CEO