# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def maxDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if root == None:
return 0
depth_l, depth_r = 0, 0
if root.left:
depth_l = self.maxDepth(root.left)
if root.right:
depth_r = self.maxDepth(root.right)
return max(depth_l, depth_r) + 1
Python, LeetCode, 104. 二叉树的最大深度
最新推荐文章于 2021-05-27 19:55:10 发布
本文介绍了一种计算二叉树最大深度的算法实现。通过递归方式遍历树的左子树和右子树,返回较大深度加一作为当前节点的最大深度。此算法适用于计算机科学中的数据结构学习及面试准备。
473

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



