class Solution:
def maxDepth(self, root: TreeNode) -> int:
def height(node):
if not node:
return 0
left = height(node.left)
right = height(node.right)
return max(left,right) + 1
return height(root)
二叉树最小深度
class Solution:
def minDepth(self, root: TreeNode) -> int:
if not root:
return 0
if not root.left and not root.right:
return 1
min_depth = 99999999999
if root.left:
min_depth = min(self.minDepth(root.left), min_depth)
if root.right:
min_depth = min(self.minDepth(root.right), min_depth)
return min_depth + 1