Given a binary tree, print its nodes level by level, i.e., print all nodes of level 1 first, followed by nodes of level 2 and so on… Print nodes for any level from left to right.

For example, the level order traversal for the following tree is 1, 2, 3, 4, 5, 6, 7:

 
Level Order Traversal

Practice this problem

We have already discussed preorder, inorder and postorder traversals of the binary tree, which are nothing but variations of Depth–first search of a Tree. Trees can also be traversed in level order, where we visit every node on a level before going to a lower level. This search is referred to as level order traversal or Breadth–first search (BFS), as the search tree is broadened as much as possible on each depth before going to the next depth.

 
A simple solution is to print all nodes of level 1 first, followed by level 2, until level h, where h is the tree’s height. We can print all nodes present in a level by modifying the preorder traversal on the tree. This is demonstrated below in C++, Java, and Python:

C++


Download  Run Code

Output:

15 10 20 8 12 16 25

Java


Download  Run Code

Output:

15 10 20 8 12 16 25

Python


Download  Run Code

Output:

15 10 20 8 12 16 25

The time complexity of the above solution is O(n2), where n is the total number of nodes in the binary tree. The auxiliary space required by the program is O(h) for the call stack, where h is the height of the tree.

We can reduce the time complexity to O(n) by using extra space. Following is a pseudocode for a simple queue-based level order traversal, which requires space proportional to the maximum number of nodes at a given depth. It can be as much as half the total number of nodes.

levelorder(root)
q —> empty queue
q.enqueue(root)
while (not q.isEmpty())
  node —> q.dequeue()
  visit(node)
  if (node.left <> null)
    q.enqueue(node.left)
  if (node.right <> null)
    q.enqueue(node.right)

The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

15 10 20 8 12 16 25

Java


Download  Run Code

Output:

15 10 20 8 12 16 25

Python


Download  Run Code

Output:

15 10 20 8 12 16 25

The time complexity of the above solution is O(n) and requires O(n) extra space, where n is the size of the binary tree.

 
We can also solve this problem by using hashing. The idea is to traverse the tree in a preorder fashion and store every node and its level in a multimap using the level number as a key. Finally, print all nodes corresponding to every level starting from the first level. We can also traverse the tree in inorder or postorder fashion.

Following is the implementation of the above approach in C++, Java, and Python:

C++


Download  Run Code

Output:

Level 1: 15
Level 2: 10 20
Level 3: 8 12 16 25
Level 4: 30

Java


Download  Run Code

Output:

Level 1: 15
Level 2: 10 20
Level 3: 8 12 16 25
Level 4: 30

Python


Download  Run Code

Output:

Level 1: [15]
Level 2: [10, 20]
Level 3: [8, 12, 16, 25]
Level 4: [30]

The time complexity of the above solution is O(n) and requires O(n) extra space, where n is the size of the binary tree.

 
Exercise: Modify the solution to print nodes of different levels in a separate line.

 
References: https://en.wikipedia.org/wiki/Tree_traversal