111 - Minimum Depth of Binary Tree
Written on October 21, 2015
Tweet
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
class Solution:
def minDepth(self, root: TreeNode) -> int:
if not root:
return 0
left = self.minDepth(root.left)
right = self.minDepth(root.right)
if left == 0:
return right + 1
elif right == 0:
return left + 1
else:
return min(left, right) + 1