Timer is Running

Reverse Level Order Traversal
Submissions: 29435   Accuracy:


  Difficulty: Easy   Marks: 2

Given a Binary Tree of size N. You have to print the Reverse Level Order Traversal of the given tree , that is first you have to print nodes of last level of the tree , then nodes of second last and so on. 
For Example: The Reverse Level Order Traversal of the following Tree is 4 5 2 3 1 .                                 

First line of input contains the number of test cases T. For each test case, there will be only a single line of input which is a string representing the tree as described below: 

  1. The values in the string are in the order of level order traversal of the tree where, numbers denotes node values, and a character “N” denotes NULL child.

  2. For example:

    For the above tree, the string will be: 1 2 3 N N 4 6 N 5 N N 7 N

For each testcase , print the reverse level order traversal of tree separated by space.

User task:
You don't need to take input. Just complete the function reversePrint() that accepts root node of the tree as parameter and prints the reverse level order traversal of tree .
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).

1 <= T <= 100
1 <= N <= 104


1 3 2
10 20 30 40 60

3 2 1
40 60 20 30 10

Testcase 1:
There are total 3 nodes of tree.

Reverse level order traversal of tree is: 3 2 1.

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Amit Khandelwal
Author: Shubham Joshi 1

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.