Python BFS solution


  • 6
    class Solution(object):
        def maxDepth(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            depth = 0
            level = [root] if root else []
            while level:
                depth += 1
                queue = []
                for el in level:
                    if el.left:
                        queue.append(el.left)
                    if el.right:
                        queue.append(el.right)
                level = queue
                
            return depth
    

    :levitate:


Log in to reply
 

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