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.Note: A leaf is a node with no children.
from collections import dequefrom leetcode_py import TreeNodeclass Solution: # Time: O(n) # Space: O(w) where w is the maximum width of the tree def min_depth(self, root: TreeNode[int] | None) -> int: if root is None: return 0 queue: deque[TreeNode[int]] = deque([root]) depth = 1 while queue: for _ in range(len(queue)): node = queue.popleft() if node.left is None and node.right is None: return depth if node.left is not None: queue.append(node.left) if node.right is not None: queue.append(node.right) depth += 1 return depth