my simple solution using level order traversal


  • 0
    A
    """
    do level order traversal. While doing traversal returns depth of the first encountered leaf node
    """
    class Solution(object):
        def minDepth(self, root):
            q = []
            depth = 0
            if root == None:
                return depth
            
            q.append(root)
            
            while q:
                depth += 1
                for _ in range(len(q)):
                    node = q.pop()
                    if node.left == None and node.right == None:
                        return depth
                    if node.left:
                        q.insert(0, node.left)
                    if node.right:
                        q.insert(0, node.right)
            
    

Log in to reply
 

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