python - 理解这个递归函数
问题描述
我得到了以下递归函数来计算二叉树的最长路径。我是递归函数的新手,有人可以帮助了解这个函数如何通过给定的示例得出结果 = 4 吗?
class Solution:
def maxDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if root is None:
return 0
else:
lDepth = self.maxDepth(root.left)
rDepth = self.maxDepth(root.right)
if lDepth > rDepth:
return lDepth+1
else:
return rDepth+1
解决方案
推荐阅读
- asp.net - 实例失败错误 SQLServer Asp Net Core
- nginx - 如何使用两个后端处理 Kubernetes 入口规则
- css - 在使用材料 ui 样式的反应 js 中是否有头部重复样式的解决方案?
- python - 如何根据用户输入连续执行带有中断条件的while循环
- node.js - 如果有人 ping 一个处于 AFK 状态的成员,它会发送一条消息 | Discord.js / mongodb
- latex - 将 tabularx 标题和单元格/列与“逐项内容”对齐加上表格边框未连接
- matlab - 根据 Matlab 中的采样时间重新采样基于时间的列数组
- python-3.x - 用于在 AWS Translate 中提取支持的语言列表的 API
- android - 在 onResume 之前不显示屏幕
- java - 为什么只有在 Heroku 上部署时才会出现 403 禁止异常?