# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def minDepth(self, root: TreeNode) -> int:
if not root: return 0
queue = []
queue.append(root)
# 根节点所在的层为第一层
depth = 1
while queue:
size = len(queue)
for i in range(size):
cur = queue[0]
queue = queue[1:]
if cur.left == None and cur.right == None:
# 到达叶子结点
return depth
if cur.left:
queue.append(cur.left)
if cur.right:
queue.append(cur.right)
depth += 1
return depth