VOOZH about

URL: https://www.geeksforgeeks.org/dsa/level-order-tree-traversal/

⇱ Level Order Traversal (Breadth First Search) of Binary Tree - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Level Order Traversal (Breadth First Search) of Binary Tree

Last Updated : 28 May, 2026

Level Order Traversal technique is a method to traverse a Tree such that all nodes present in the same level are traversed completely before traversing the next level.

Input:

👁 112

Output: [[5], [12, 13], [7, 14, 2], [17, 23, 27, 3, 8, 11]]
Explanation: Start with the root - [5]
Level 1: [12, 13]
Level 2: [7, 14, 2]
Level 3: [17, 23, 27, 3, 8, 11]

[Approach] Using Recursion - O(n) time and O(n) space

The idea is to traverse the tree recursively, starting from the root at level 0. When a node is visited, its value is added to the result array at the index corresponding to its level, and then its left and right children are recursively processed in the same way. This effectively performs a level-order traversal using recursion.


Output
5 
12 13 
7 14 2 
17 23 27 3 8 11 

[Expected Approach] Using Queue (Iterative) - O(n) time and O(n) space

The idea is to use a queue to traverse the tree level by level. Start by adding the root to the queue. Then, repeatedly remove a node from the queue, store its value in the result, and add its left and right children to the queue. Continue this process until the queue is empty.


Output
5 
12 13 
7 14 2 
17 23 27 3 8 11 
Comment