Print Binary Tree levels in sorted order
Submissions: 4836   Accuracy:

24.35%

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

Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order.

Input:
The first line of the input contains integer T denoting the number of test cases. For each test case, the first line takes an integer n denoting the size of array i.e number of nodes followed by n-space separated integers denoting the nodes of the tree in level order fashion.

Output:
For each test case, the output is the level order sorted tree.
Note:  For every level, we only print distinct elements.

Constraints:
1 <= T <= 100
1 <= n <= 104
1 <= arr[i] <= 104

Example:
Input:

2
7
7 6 5 4 3 2 1
6
5 6 4 9 2 1 
Output:
7
5 6
1 2 3 4
5
4 6
1 2 9

Explanation:
Testcase 1:

Tree looks like this   

            7
          /    \
        6       5
       / \     / \
      4  3    2   1
Sorted order:
7
5 6
1 2 3 4
          

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

Contributor: Vanshika
Author: Vanshika_pec


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.