In DFS traversal of a binary tree, we access nodes in three different orders: preorder, postorder, and inorder. Now there is another traversal that can access nodes in level-by-level order. This is called level order traversal or breadth-first search. In the short form, we also call it BFS traversal.
A binary tree is organised into different levels where root is at the topmost level. So one idea is to traverse tree level by level i.e. start from the root node (level 0), then process all nodes at the first level, second level, and so on. Here we explore all nodes at the current level before moving on to nodes at the next level.
The idea of level order traversal using queue
Let's observe the order of nodes in level order traversal: We first process the root node at level 0, and then we process the left and right child at level 1 (left to right order). Similarly at the second level, we first process children of the left child of the root then process children of the right child. This process goes on for all levels.
Let's observe another pattern: If we first process a node x at any given level, then the children of node x will be processed first at the next level. From another perspective: If node x comes before node y at the same level, then at the next level, we will process the children of node x before processing the children of node y. This is the First In First Out (FIFO) order. So we can use a queue to simulate the level order traversal.
Implementation steps
Step 1: We create an empty queue Q and initialize it with the root.
Step 2: Now we run a loop until the Q is empty. Inside the loop, we declare a variable currNode to keep track of the current node during the traversal.
- We start the loop by removing the front node from the Q and assigning it to currNode.
- Now we process currNode and insert its left and right child into the Q if they exist: 1) If the left child of currNode is not NULL, insert it into the Q 2) If the right child of currNode is not NULL, insert it into the Q.
Implementation pseudocode
levelOrderTraversal(root)
if root is null
return
Q = empty queue
enqueue root
while Q is not empty
currNode = dequeue Q
process currNode.data
if currNode.left is not null
enqueue currNode.left
if currNode.right is not null
enqueue currNode.rightTime complexity analysis
Suppose n number of nodes are given in the input.
- The time complexity of each enqueue and dequeue operation = O(1).
- We are doing two queue operations for each node: Inserting once into the queue and deleting once from the queue. So total queue operations = 2n.
- Time complexity = Total queue operations * Time complexity of one queue operation = 2n * O(1) = O(n)
Space complexity analysis
Space complexity is equal to the queue size. We process nodes level by level, so the max queue size depends on the level with the max number of nodes or max-width of the binary tree. If the max width of the binary tree is w, space complexity = O(w). The idea is simple: w depends on the structure of a given binary tree.
Worst case: When tree is balanced
When tree is balanced, the last level will have maximum width or maximum number of nodes, which will be 2^h (where h is the height of the tree). For balanced tree, h = logn, size of the queue = O(2^h) = O(2^(log n)) = O(n). Space complexity = O(n).
Best case: When tree is skewed
In such case, every level will have one node. So at any point, there will be at most one node in the queue. So size of the queue = O(1). Space complexity = O(1).
BFS vs DFS traversal of binary tree
- Both traversals require O(n) time as they visit every node exactly once.
- Depth-first traversal starts from the root, goes to the depth as far as possible and then backtracks from there. In breadth-first search, we explore nodes level by level i.e. in order of their distance from the root node. So if problem is to search for something closer to the root, we can prefer BFS. If we need to search for something in the depth of tree or node closer to leaf, we can prefer DFS.
- In BFS traversal, we use queue to store nodes of different levels. In DFS traversal, we use the stack (If recursive, system use call stack) to store all ancestors of a node.
- The memory taken by both BFS and DFS depends on the structure of tree. Extra space required for BFS is O(w), but additional space needed for DFS Traversal is O(h). Here w = maximum width of binary tree and h = maximum height of binary tree. In the worst case, both require O(n) extra space, but worst cases occur for different types of trees. For example, space needed for BFS Traversal is likely to be more when a tree is more balanced, and extra space for DFS Traversal is likely to be more when a tree is less balanced.
- When node order is not required for the solution of a given problem, we can use both BFS and DFS traversals. In some cases, such things are not possible. We need to identify the correct traversal to solve the problems efficiently.
Problems to practice using BFS traversal
- Level order traversal in spiral form
- Reverse Level Order Traversal
- Left View of Binary Tree
- Maximum width of a binary tree
- Min depth of binary tree
- Level with the maximum number of nodes
- Count the number of nodes at a given level
- Convert a binary tree into a mirror tree
If you have any queries or feedback, please write us at contact@enjoyalgorithms.com. Enjoy learning, Enjoy coding, Enjoy algorithms!

