


1. The structure of the city is already defined. You should not change it.
2. The address of the house at the 0’th level is already given.
3. There is at least one house in the city.
4. You may only use constant extra space.
The first line of input contains an integer ‘T’ denoting the number of test cases.
Then the description of ‘T’ test cases follows.
The first line of each test case contains elements in the level order form. The line consists of a HOUSENUMBER of houses separated by a single space. In case there is no House on the left, right or adjacent then set NULL in those pointers. house is null, we take -1 in its place.
For example, the input for the tree depicted in the below image would be :
1
2 3
-1 4 -1 5
-1 -1 -1 -1
Level 1 :
The House at 0’th level has HOUSENUMBER 1
Level 2 :
House on the Left of HOUSENUMBER 1 is 2
House on the Left of HOUSENUMBER 1 is 3
Level 3 :
There is no House on the Left of HOUSENUMBER 2 so we have -1
House on the Right of HOUSENUMBER 2 is 4
There is no House on the Left of HOUSENUMBER 3 so we have -1
House on the Right of HOUSENUMBER 3 is 5
Level 4 :
There is no house on the left and right of the 4 and 5 so we have all -1 on this level
The above format was just to provide clarity on how the input is formed for a given tree.
The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:
1 2 3 -1 4 -1 5 -1 -1 -1 -1
For each test case, print a single line containing the level order as connected by the next pointers, with '#' signifying the end of each level.
The output of each test case will be printed in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= N <= 3000
-10 ^ 9 <= HOUSENUMBER <= 10 ^ 9
Where ‘T’ is the total number of test cases, ‘N’ is the number of Houses in the given city and 'HOUSENUMBER' is the House number of each House.
Time limit: 1 sec.
We will make an inorder traversal on the city and make a vector which will store the address of the House. Each level vector will contain the address of a house that was previously processed. We update their next point with the current house and finally update the value at ‘index’ = ‘level’ as current House.
For each level do the following:
In the Level order traversal or breadth first traversal of the tree, we traverse all the houses at each level before going to the next level. We can extend level order traversal to solve this problem as follows.
Suppose we have already connected the Houses till level ‘L’. Now we can iterate over level ‘L’ using ‘next’ pointers and connect houses of level ‘L+1’.
The complete algorithm follows given steps -: