Reverse alternate levels of a perfect binary tree
Submissions: 5207   Accuracy:

40.11%

  Difficulty: Easy   Marks: 2
Associated Course(s):   Sudo Placement 2019

Write a function to reverse alternate levels  of a perfect binary tree.

For example
 
                                           1     should be Modified to       1
                                         /     \       - - - - - - - - - - - - >        /   \
                                        2     3                                         3     2

Inorder traversal  : 3 1 2 

 

Input:
The task is to complete the method which takes one argument, root of the Tree. The struct node has a data part which stores the data, pointer to left child and pointer to right child.
There are multiple test cases. For each test case, this method will be called individually.


Output:
The function should modify the root of tree such that the alternate levels of the binary tree are reversed .The output generated for the expected output will be the inorder traversal of the modified tree.


Constraints:
1 <=T<= 30
1 <=Number of nodes<= 100
1 <=Data of a node<= 1000


Example:
Input:

2
2
1 2 R 1 3 L
6
1 2 L 1 3 R  2 42 L 2 51 R 3 63 L 3 72 R

Output:
2 1 3
42 3 51 1 63 2 72 

There are two test casess.  First case represents a tree with 3 nodes and 2 edges where root is 1, left child of 1 is 3 and right child of 1 is 2.   Second test case represents a tree with 6 edges and 7 nodes.


 

** 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.