Python BFS FIFO Solution


  • 0
    C
    class Solution(object):
        def minDepth(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            if not root:
                return 0
            queue=[root]
            eachlevel=1
            size=1
            while queue:
                if size==0:
                    size=len(queue)
                    eachlevel+=1
                temp=queue.pop(0)
                #print temp.val
                size-=1
                if not temp.left and not temp.right:
                    break
                if temp.left:
                    queue.append(temp.left)
                    
                if temp.right:
                    queue.append(temp.right)
                    
                
                
            return eachlevel
    

Log in to reply
 

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