# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = Noneclass Solution:
def maxDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
#递归
if root is None:
return 0
left_height = self.maxDepth(root.left)
right_height = self.maxDepth(root.right)
return max(left_height,right_height)+1
leetcode - 104 - 二叉树的最大深度
本文介绍了一种通过递归算法计算二叉树最大深度的方法。该算法首先判断根节点是否为空,若为空则返回深度为0;否则分别计算左右子树的深度,返回较大值加1作为整棵树的深度。

被折叠的 条评论
为什么被折叠?



