Link:102. Binary Tree Level Order Traversal - Medium
Track: NeetCode150

Question

Restate the problem

Given the root of a binary tree, return the level order traversal of its nodes’ values.
That is, return a list of lists, where each inner list contains the values of nodes from left to right at the same depth/level.


Method 1 - BFS
Method 2 - DFS

Method

Approach

Complexity

  • Time Complexity: O(n)
  • Space Complexity: O(w)

Edge Case

  • if no root, return []

Code

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
        if not root:
            return []
        queue = deque([root])
        res = []
        while queue:
            size = len(queue)
            level = []
            for _ in range(size):
                node = queue.popleft()
                level.append(node.val)
                
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
            
            res.append(level)
        return res

History

Jan-24-2026 Sloved