Logo for ammarahmed.ca
Medium
Feb 18, 2026
#binary-tree
#queue

102. Binary Tree Level Order Traversal

Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level).

Example 1:

1Input: root = [3,9,20,null,null,15,7] 2Output: [[3],[9,20],[15,7]]

Example 2:

1Input: root = [1] 2Output: [[1]]

Example 3:

1Input: root = [] 2Output: []
  • The number of nodes in the tree is in the range [0, 2000].
    • -1000 <= Node.val <= 1000

      Constraints:

      Notes

      Intuition

      Level order traversal of binary tree is done with queue, but we need to process all the same level together.

      Implementation

      Basic level order traversal where we simply want to print the values or do something with each level sequentially is quite simple with a queue, just dequeue process and then push the right and left if they exist. However, in this problem, we want to add all values from a given level to an array and then add that array to the result, therefore, we can't simply store the nodes by individually in the queue. Each queue element should contain all nodes for that level.

      The queue will consist of an array of nodes for that level. Start off by initializing the queue with its first element being an array containing just the root (level1). Then, iterate while the queue is not empty, dequeue the nodes from the queue. Initialize a temporary array for the values that will be pushed to the result and another array for the level nodes that will be pushed to the queue after processing the current nodes. Iterate over the nodes, add the value to the temporary array. If the node has left, add that node to the level nodes array, same with right. After this iteration, we add the temporary array to the result and check if the level nodes are not empty and add them to the queue.

      Edge-cases

      If the root is null, return empty array early.

      • Time: O(n): we process each node once.
        • Space: O(n): the queue can contain all the nodes at worst

          Complexity

          Solution

          1from typing import List, Optional 2from lcutils import TreeNode 3from collections import deque 4 5# Definition for a binary tree node. 6# class TreeNode: 7# def __init__(self, val=0, left=None, right=None): 8# self.val = val 9# self.left = left 10# self.right = right 11class Solution: 12 def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: 13 result = [] 14 if not root: 15 return result 16 17 q = deque([[root]]) 18 while q: 19 nodes = q.popleft() 20 temp = [] 21 level_nodes = [] 22 for node in nodes: 23 temp.append(node.val) 24 if node.left: 25 level_nodes.append(node.left) 26 if node.right: 27 level_nodes.append(node.right) 28 if level_nodes: 29 q.append(level_nodes) 30 result.append(temp) 31 32 return result 33 34 35if __name__ == "__main__": 36 # Include one-off tests here or debugging logic that can be run by running this file 37 # e.g. print(solution.two_sum([1, 2, 3, 4], 3)) 38 solution = Solution() 39 root = TreeNode.create(1,2,3,4,None,None,5) 40 print(str(root)) 41 42