根据二叉树的特点,前序遍历整棵树,遇到叶子节点记录整条路径上的节点数;用一个全局变量记录整棵树的最小深度,注意判空

# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param root TreeNode类 
# @return int整型
#
class Solution:
    def __init__(self):
        self.depth = float("inf")
        
    def run(self , root: TreeNode) -> int:
        # write code here
        if not root:
            return 0
        self.sub_run(root, 1)
        return self.depth
            
    
    def sub_run(self, root: TreeNode, depth: int) -> int:
        if not root.left and not root.right:
            self.depth = min(self.depth, depth)
            return
        if root.left:
            self.sub_run(root.left, depth + 1)
        if root.right:
            self.sub_run(root.right, depth + 1)