Python BFS Non-recursive solution


  • 0
    class Solution(object):
        def minDepth(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            if not root:
                return 0
            
            from collections import deque
            dq = deque()
            dq.append(root)
            level = 1
            while dq:
                lenDQ = len(dq)
                j = 0
                while j < lenDQ:
                    current = dq.popleft()
                    if current.left is None and current.right is None:
                        return level
                    if current.left:
                        dq.append(current.left)
                    if current.right:
                        dq.append(current.right)
                    j += 1
                level += 1
            return level
    

Log in to reply
 

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