# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

#
# 
# @param root TreeNode类 
# @return int整型
#
class Solution:
    def run(self , root ):
        # write code here
        if root == None:
            return 0
        def min_num(a,b):
            if a>b:
                return b
            return a
        
        #第一版直接递归方法
        # def min_depth(node):
        #     if node == None:
        #         return 0
        #     if node.left == None and node.right == None:
        #         return 1
        #     left = right = None
        #     if node.left != None and node.right != None:
        #         left = min_depth(node.left)
        #         right = min_depth(node.right)
        #         return min_num(left,right) + 1
        #     if node.left != None:
        #         return min_depth(node.left) + 1
        #     if node.right != None:
        #         return min_depth(node.right) + 1
        # min = min_depth(root)
        # return min
        
        #优化后的剪枝方法
        global now_min
        now_min = 99999999999999999999
        def min_depth(node,father_depth):
            global now_min
            if node == None:
                now_min = 0
                return now_min
            father_depth += 1
            if father_depth >= now_min:#剪枝
                return now_min            
            if node.left == None and node.right == None:#当前节点是叶子节点
                now_min = min_num(now_min,father_depth)
                return now_min
            left = right = None
            if node.left != None and node.right != None:
                left = min_depth(node.left,father_depth)
                right = min_depth(node.right,father_depth)
                now_min = min_num(left,right)
                return now_min
            if node.left != None:
                now_min = min_depth(node.left,father_depth)
                return now_min
            if node.right != None:
                now_min = min_depth(node.right,father_depth)
                return now_min     

        min = min_depth(root,0)
        return min