# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def minDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if root is None:
return 0
leftDepth = self.minDepth(root.left)
rightDepth = self.minDepth(root.right)
if root.left is None or root.right is None:
return max(leftDepth, rightDepth) + 1
return min(leftDepth, rightDepth) + 1
Python, LeetCode, 111. 二叉树的最小深度
最新推荐文章于 2024-03-04 21:39:55 发布