A Python3 Solution

  • 0

    One problem must be enphasized is that ' from the root node down to the nearest leaf node'. It means that when the root.right or root.left is None, its path should be ignored.

    class Solution:
    def minDepth(self, root):
    :type root: TreeNode
    :rtype: int
    if root == None:
    return 0
    if root.left == None:
    return self.minDepth(root.right) + 1
    if root.right == None:
    return self.minDepth(root.left) + 1
    Depth_l = self.minDepth(root.left)
    Depth_r = self.minDepth(root.right)
    return min(Depth_l, Depth_r) + 1

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.