# 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: Optional[TreeNode]) -> int:
def dfs(node):
if not node:
return 0
if node.left and not node.right:
return 1 + dfs(node.left)
elif not node.left and node.right:
return 1 + dfs(node.right)
elif not node.left and not node.right:
return 1
elif node.left and node.right:
return 1 + min(dfs(node.left), dfs(node.right))
res = dfs(root)
return res