Print Binary Tree levels in sorted order
Easy Accuracy: 29.47% Submissions: 6091 Points: 2

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
          

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.