# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def maxDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if root is None:
return 0
return max(self.maxDepth(root.left),self.maxDepth(root.right))+1
【leetcode】104. Maximum Depth of Binary Tree
最新推荐文章于 2025-05-05 16:31:47 发布
本文介绍了一种计算二叉树最大深度的算法实现。通过递归的方式,该算法能够遍历二叉树的所有节点,并返回从根节点到最远叶子节点的最长路径长度。
443

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



