Maximum Depth of Binary Tree

Binary Tree
Easy

Approach

Diagrams

Analysis

Time

O(n)

Space

O(n)
class Solution:
    def maxDepth(self, root):
        if root is None:
            return 0

        return self.levelOrderTraversal(root, 0)

    def levelOrderTraversal(self, root, depth):
        queue = deque([root])

        while queue:
            nodesInCurLevel = len(queue)

            for _ in range(nodesInCurLevel):
                node = queue.popleft()

                for child in [node.left, node.right]:
                    if child is not None:
                        queue.append(child)

            depth += 1

        return depth

Learn

#Level Order Traversal

Videos