Binary Tree Level Order Traversal
Level Order Traversal is a Breadth-First Search (BFS) strategy used to visit each node in a binary tree in hierarchical order. Specifically, it starts from the root node and traverses all nodes level by level, from top to bottom and left to right. This traversal method is very useful for solving problems such as "Maximum Depth of Binary Tree," "Minimum Depth of Binary Tree," and "Symmetric Tree."
1. Core Idea and Basic Steps
The core idea of level order traversal is to use a queue data structure for assistance. The First-In-First-Out (FIFO) property of a queue perfectly matches the requirement of accessing nodes in hierarchical order. The basic steps are as follows:
- Initialization: Add the root node to the queue.
- Process the Queue in a Loop: As long as the queue is not empty, repeat the following steps:
- Dequeue the current node (i.e., the front node) and visit it (e.g., print its value).
- If the current node has a left child, add the left child to the queue.
- If the current node has a right child, add the right child to the queue.
- Termination Condition: When the queue is empty, all nodes have been processed.
2. Detailed Process Example
Consider the following binary tree:
1
/ \
2 3
/ \ \
4 5 6
The result of the level order traversal should be: 1, 2, 3, 4, 5, 6.
Step-by-Step Simulation:
- Step 1: The initial queue is
[1].- Dequeue node 1, visit node 1 (output
1). - Add node 1's left child (node 2) and right child (node 3) to the queue, making the queue
[2, 3].
- Dequeue node 1, visit node 1 (output
- Step 2: The queue is not empty. Dequeue node 2, visit node 2 (output
2).- Add node 2's left child (node 4) and right child (node 5) to the queue, making the queue
[3, 4, 5].
- Add node 2's left child (node 4) and right child (node 5) to the queue, making the queue
- Step 3: Dequeue node 3, visit node 3 (output
3).- Add node 3's right child (node 6) to the queue, making the queue
[4, 5, 6].
- Add node 3's right child (node 6) to the queue, making the queue
- Step 4: Dequeue node 4, visit node 4 (output
4). Node 4 has no children, so the queue becomes[5, 6]. - Step 5: Dequeue node 5, visit node 5 (output
5). Node 5 has no children, so the queue becomes[6]. - Step 6: Dequeue node 6, visit node 6 (output
6). Node 6 has no children, so the queue becomes empty. - Termination: The queue is empty, and the traversal ends. The final output order is
1, 2, 3, 4, 5, 6.
3. Code Implementation (Pseudocode)
Describing the algorithm logic in a general programming language style:
LevelOrderTraversal(root):
if root is null, return
initialize queue, add root to queue
while queue is not empty:
node = dequeue front element
visit node's value
if node has left child, add left child to queue
if node has right child, add right child to queue
4. Common Variant: Grouping Output by Level
Sometimes interviews require outputting the values of each level as separate lists (e.g., LeetCode problem "Binary Tree Level Order Traversal"). In this case, slight adjustments are needed:
- Before processing each level, record the current length of the queue (i.e., the number of nodes at that level).
- Process all nodes of the current level in a loop and store their values in a temporary list.
- Add the temporary list to the result set before proceeding to the next level.
Adjusted Pseudocode:
LevelOrderTraversalGrouped(root):
if root is null, return empty list
initialize queue, add root to queue
initialize result list
while queue is not empty:
level_size = current length of queue
initialize level_list
repeat level_size times:
node = dequeue front element
add node's value to level_list
if node has left child, add to queue
if node has right child, add to queue
add level_list to result
return result
For the example binary tree above, the grouped output result would be: [[1], [2, 3], [4, 5, 6]].
5. Time Complexity and Space Complexity
- Time Complexity: O(n), where n is the number of nodes, as each node is visited exactly once.
- Space Complexity: O(n), as the queue may store up to one level of nodes at a time, which in the worst case (balanced binary tree) is roughly O(n).
By following these steps, you can master the basic principles, implementation details, and common application scenarios of level order traversal.