1. 程式人生 > >leetcode11 二叉樹的最小深度

leetcode11 二叉樹的最小深度

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:

    def minDepth(self, root):
        if root is None:
            return 0
        if root.left is None and root.right is None:
            return 1
        if root.left and root.right is None:
            return self.minDepth(root.left) + 1
        if root.left is None and root.right:
            return self.minDepth(root.right) + 1
        if root.left and root.right:
            return min(self.minDepth(root.left), self.minDepth(root.right)) + 1