Reverse level order traversal of binary tree in java

If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions.

This is part of java binary tree tutorial.
In this post, we will see about Reverse Level Order binary tree traversal in java. In previous post, we have already seen Level order traversal. In reverse level order traversal, we will visit last level first, then second last and eventually first level.

Reverse Level Order traversal:

Reverse Level order traversal of below binary tree will be:

We will use stack  for Reverse Level Order traversal.

Steps for Reverse Level order traversal algorithm:

  1. Create empty queue and push root node to it.
  2. Do the following when queue is not empty
    • Pop a node from queue and print it
    • Push right child of popped node to queue if not null
    • Push left child of popped node to queue if not null
    • Push popped node to stack
  3. Pop node from stack and print it
Example:
Lets say your binary tree is :

So Reverse Level Order traversal will work as below:

Lets create java program :

Run above program and you will get following output:

Reverse Level Order traversal of binary tree will be:
10 30 50 70 20 60 40

Java Binary tree tutorial

Please go through java interview programs for more such programs.


import_contacts

You may also like:

Related Posts

  • How to print an array in PHP
    19 October

    How to print an array in PHP

    Table of ContentsIntroductionHow to print an array in PHP?1) print_r() function:2) var_dump() function:3) json_encode() function:4) foreach() loop: Introduction An array is one kind of data structure, which can store multiple items of related data types. The printing of an array is one of the fundamental functions of PHP. How to print an array in PHP? […]

  • Multiple classes in one file in Java
    18 October

    Multiple classes in one file in Java

    Table of ContentsIntroductionMethods to Implement Multiple Classes In One Java Program1) Nested classes2) Multiple non-static nested classes In this post, we will see how to have multiple classes in one file in java. Introduction You need to have any number of classes in a single Java file, but there is a restriction that you can […]

  • How to initialize empty array in PHP
    18 October

    Declare empty array in php

    Table of ContentsUsing square brackets to declare empty array in PHPUsing array() to declare empty array in PHP An array is a data structure where you can store the same or different types of data in a single variable. In PHP, you can declare empty array using square brackets ([]) or using array() function. Using […]

  • Find duplicate elements in the Stream
    17 October

    Java 8 – Find duplicate elements in Stream

    Table of ContentsIntroductionUsing distinct()Using Collections.frequency()Using Collectors.toSet()Using Collectors.toMap()Using Collectors.groupingBy()Conclusion Introduction When working with a collection of elements in Java, it is very common to have duplicate elements, and Java provides different APIs that we can use to solve the problem. Java 8 Stream provides the functionality to perform aggregate operations on a collection, and one of […]

  • 17 October

    Java AES 256 Encryption Decryption Example

    Table of ContentsIntroductionGenerate a shared keyEncrypt a random textDecrypt the encrypted textConclusion Introduction Advanced encryption standard (AES) is the most secure encryption standard compared to RSA, which is vulnerable to brute force attacks. This is the main reason that AES was established by the National Institute of Standard and Technology (NIST) in 2001 and is […]

  • 16 October

    Java AES Encryption Decryption Example

    Table of ContentsIntroductionGenerate a shared keyEncrypt a random textDecrypt the encrypted textConclusion Introduction AES stands for advanced encryption standard and is the most commonly used symmetric algorithm to encrypt sensitive data and can be used in both software and hardware. The AES algorithm is symmetric, meaning that it uses only one key for encryption and […]

Comments

  1. Great post, thanks for writing this material

    There is a small mistake, you have as a first step in algorithm “Pop a node from queue and print it”
    I'm guessing that's a typo or copy and paste from level order traversal

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.