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.
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
Code:
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
//code
int t;
cin>>t;
while(t--)
{priority_queue<int,vector<int>,greater<int>> m;
int a;
cin>>a;
int k=1;
int arr[a];
queue<int> s;
for(int i=0;i<a;i++)
{
cin>>arr[i];
s.push(arr[i]);
}
while(s.empty()==false)
{
if(m.size()==k)
{
while(m.empty()==false)
{
cout<<m.top()<<" ";
}
k=k*2;
cout<<endl;
}
else
{
m.push(s.front());
s.pop();
}
}
if(m.empty()==false)
{
while(m.empty()==false)
{
cout<<m.top()<<" ";
}
cout<<endl;
}
}
return 0;
}
ERROR:OUTPUT LIMIT EXCEEDED!!!