VOOZH about

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

⇱ Reverse Level Order Traversal - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Reverse Level Order Traversal

Last Updated : 23 Jul, 2025

Given a binary tree, the task is to find its reverse level order traversal. The idea is to print the last level first, then the second last level, and so on. Like Level order traversal, every level is printed from left to right.

Examples:

Input:

👁 Image

Output:
4 5
2 3
1

[Naive Approach] Using Recursion - O(n^2) Time and O(h) Space:

The algorithm for reverse level order traversal involves printing nodes level by level, starting from the bottom-most level and moving up to the root. First, the height of the tree is determined, denoted as h. The traversal then begins from level h and proceeds upwards to level 1. In each iteration, printGivenLevel function is called to print nodes at the current level. It prints the node's data if the current level matches the required level. If not, the function recursively explores the left and right subtrees to find and print nodes at the required level.


Output
4 5 2 3 1 

Time Complexity:  O(n^2)
Auxiliary Space: O(h), where h is the height of the tree.

[Expected Approach] Using Stack and Queue - O(n) Time and O(n) Space:

The approach for reverse level order traversal, using a stack and queue is conceptually similar to a standard level order traversal but with key modifications. Instead of printing each node as it is visited, the nodes are pushed onto a stack. Additionally, when enqueueing children nodes, the right child is enqueued before the left child. This ensures that when nodes are popped from the stack, they are printed in reverse level order.

Step by step implementation:

  1. The idea is to use a stack to get the reverse level order.
  2. Push the root node into a queue. While queue is not empty, perform steps 3,4.
  3. Pop the front element of the queue. Instead of printing it (like in level order), push node's value into a stack. This way the elements present on upper levels will be printed later than elements on lower levels.
  4. If the right child of current node exists, push it into queue before left child. This is because elements on the same level should be printed from left to right.
  5. While stack is not empty, pop the top element and print it.

Output
4 5 2 3 1 

Time Complexity: O(n), where n is the number of nodes in the binary tree. 
Auxiliary Space: O(n)

[Alternate Approach] Using a hash map - O(n) Time and O(n) Space:

The approach to reverse level order traversal using a hash map involves storing nodes by their levels and then retrieving them in reverse order. To achieve this, a hash map is used where each level of the binary tree maps to a list of nodes at that level. During a pre-order traversal of the tree, nodes are added to the hash map based on their level, ensuring that nodes are processed from the leftmost node at each level first. If the current node is null, the function returns immediately. Otherwise, it adds the node to the list corresponding to its level in the hash map, then recursively processes the left and right children.

After completing the traversal, the height of the tree, denoted by the size of the hash map, determines the number of levels. The final step is to iterate over the hash map from the highest level down to level 1, printing nodes from each level to achieve the reverse level order traversal.

Below is the implementation of the above approach:


Output
4 5 2 3 1 

Time complexity: O(n), where n is the number of nodes in the binary tree.
Auxiliary Space: O(n)

Comment