Reverse Level Order Traversal

By | December 30, 2022

If you are a budding programmer, trying to get your hands on coding concepts, you can’t escape the concept of Reverse level order traversal.

Traversing a binary tree using level order traversal is a topic that you may often encounter in your binary tree study.

Though, you may vaguely know of this concept, it is better to have a proper detailing to implement it better.

To know it better, read this article and implement the concept with help of a problem statement.

What is Reverse Level Order Traversal?

A reverse level order traversal is a way of traversing a tree where the nodes are visited in a specific order. In a normal level order traversal, the nodes of a tree are visited from left to right, starting at the top level and moving down to the bottom level.

In a reverse level order traversal, the nodes are visited from right to left, starting at the bottom level and moving up to the top level.

For example, consider the following tree:

 1

/

2 3

/ \ /

4 5 6 7

A reverse level order traversal of this tree would visit the nodes in the following order: 7, 6, 5, 4, 3, 2, 1.

One way to implement this technique is to use a queue to store the nodes at each level. The algorithm would work as follows:

  • Start at the root node and enqueue it into the queue.
  • Dequeue the node at the front of the queue and visit it.
  • Enqueue the node’s children (if any) into the queue.
  • Repeat steps 2 and 3 until the queue is empty.
  • Reverse the order of the nodes at each level and repeat the process until the tree has been fully traversed.

In this way, the algorithm is able to visit the nodes of the tree in reverse level order, starting from the last level and moving towards the root.

Let’s understand this approach with a problem statement.

Problem Statement

Return the values of a binary tree’s nodes when given in reverse level order. (i.e., from the left to right and initial level to last level).

Examples:

Input: 9

3

20

15

7

Output: [15, 7, 9, 20, 3]

Explanation:

  • Nodes as level 3 : [15, 7]
  • Nodes at level 2: [9, 20]
  • Nodes at level 1: [3]
  • Reverse level order traversal will be: [15, 7, 9, 20, 3]

Approaches to solve reverse level order traversal

Using Stack

There are several ways to solve the problem of reverse level order traversal of a tree. One approach is to use a stack to store the nodes at each level as they are traversed in a breadth-first manner.

Then, when all the nodes at a given level have been added to the stack, they can be popped off and printed in reverse order.

Using Queue

Reverse level order traversal is a method of traversing a tree in which the nodes are visited in the reverse order of the levels of the tree. This means that the nodes on the lowest level of the tree are visited first, followed by the nodes on the second-lowest level. It will continue until the root node is visited last.

To implement this technique, we can use a queue to store the nodes of the tree in the order that they are visited. We can then process the nodes in the queue in the reverse order in which they were added, starting with the last node and working our way towards the first.

As for valid parentheses, it refers to a string of parentheses (or any other type of bracket) that is properly balanced. This means that for every opening bracket, there is a corresponding closing bracket, and that the brackets are properly nested.

For example, the string “(())” is valid, while the string “(()” is not, because the first opening bracket does not have a corresponding closing bracket.

Here is some pseudocode for the second approach:

# Initialize two queues, currLevel and nextLevel

currLevel = empty queue

nextLevel = empty queue

# Add the root node to currLevel

currLevel.enqueue(root)

# Loop while currLevel is not empty

while (not currLevel.isEmpty()):

  # Dequeue the node at the front of currLevel and print its value

  node = currLevel.dequeue()

  print(node.value)

  # Enqueue the children of node to nextLevel

  if (node.left is not null):

    nextLevel.enqueue(node.left)

  if (node.right is not null):

    nextLevel.enqueue(node.right)

  # If currLevel is empty, swap currLevel and nextLevel and print a newline

  if (currLevel.isEmpty()):

    currLevel, nextLevel = nextLevel, currLevel

    print()

Using Recursion

Another way to perform a reverse level order traversal using recursion is to first create a recursive function that takes a tree node as an input and visits all of the nodes in the subtree rooted at that node in reverse level order.

 This function would first visit the right subtree of the input node, then the left subtree, and finally the input node itself.

Here is an example of a recursive function that performs a reverse level order traversal:

def reverse_level_order_traversal(node):

    if node is not None:

        # Visit the right subtree

        reverse_level_order_traversal(node.right)

        # Visit the left subtree

        reverse_level_order_traversal(node.left)

        # Visit the input node

        print(node.val)

This function can be used to traverse a tree in reverse level order by calling it on the root node of the tree.

For example, if the tree looks like this:

    1

   / \

  2   3

 / \   \

4   5   6

The output of the reverse_level_order_traversal() function would be:

4

5

2

6

3

1

This is because the function first visits the right subtree of the root node (which contains the node with value 6), then the left subtree (which contains the nodes with values 2 and 5), and finally the root node itself (which has value 1).

Conclusion

With these approaches, you can solve any problem statement that involves Reverse level order traversal. You can implement these codes easily in different programming languages. If you are clear with the concept, go ahead with it.

You can crack the problem statement in a few seconds. Prepare related questions in advance to become an expert when it comes to these reversal order problems.

Category: Technology

About Joshua Taylor

Joshua is the Senior Writer at VOIVO Infotech. He covers news on Android and iOS Apps. He is also a design director at Credit Karma. He is excited about the latest startups in the market. He constantly thrives to work on new ideas and projects. To get in touch with Joshua for news reports you can email him on [email protected] or reach him out on social media links given below.