Python Solution


  • 1
    G

    My solution using Breadth-First-Search

    class Solution(object):
        def minDepth(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            if not root:
                return 0
            res, queue=0, collections.deque([(root,0)])
            while queue:
                node, length=queue.popleft()
                if not node.left and not node.right:
                    length+=1
                    if res==0:
                        res=length
                    else:
                        res=min(res,length)
                    continue
                if node.left:
                    queue.append((node.left, length+1))
                if node.right:
                    queue.append((node.right,length+1))
            return res
    

Log in to reply
 

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