Beautiful City

Easy
0/40
Average time to solve is 10m
3 upvotes
Asked in companies
AmazonGoldman SachsSamsung

Problem statement

Ninja decided to travel to a city. Each house in the city is connected via roads with at most two houses and forms a binary tree-like structure such that Kth level can have at most 2 ^ K houses. Now, the problem in the city is that persons from different houses at the same level can not meet without changing their level. Ninja wants to make this city beautiful by connecting houses at the same level. As Ninja is busy preparing for his travel, he gives this task to you. Can you help Ninja to make this city beautiful?

For example Figure A represents the city before making it Beautiful. Figure B represents a Beautiful city because all the houses on the same level are connected.

alt txt alt txt

Your task is to connect all the houses at the same level in the city. You are given a pointer next in the house class to do this. If there is no house on the right then set NULL to the next pointer.

Note:
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.
Detailed explanation ( Input/output format, Notes, Images )
Input format:
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 :

alt test1

 1
 2 3
-1 4  -1 5 
-1 -1 -1 -1

Explanation :

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
Note :
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
Output format :
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.

Note:

You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
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.
Sample Input 1:
1
1 2 3 -1 4 -1 5 -1 -1 -1 -1
Sample Output 1:
1 # 2 3 # 4 5 #   
Explanation of Sample Input 1:
Test case 1:
The level order traversal of a given city is 1 2 3 -1 4 -1 5 -1 -1 -1 -1, thus its first House is HOUSENUMBER with value 1, and left and right Houses of the 1 are nodes with 2 and 3 respectively.
The initial binary tree and the binary tree after connecting adjacent nodes are shown below

alt test1 alt testout

Sample Input 2:
1
1 2 -1 -1 4 -1 -1
Sample Output 2:
1 # 2 # 4 # 
Explanation of Sample Input 2:
See the figures below for their explanation.

alt out2 alt out1

Hint

Use inorder traversal and store addresses of House at every level in some data structure.

Approaches (3)
Recursion

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:

  • If the current level is getting processed for the first time then store the address of the current House in the vector.
  • If the current level is not getting processed for the first time then update the value of the next pointer of the House at the ‘index’ = ‘level’  of the current level in vector.
  • Finally, update the  ‘index’ = ‘level’ as current house

 

Time Complexity

O(N), where ‘N’ is the number of Houses in the city.

 

We are visiting all the houses in the city once. So, the time complexity is O(N).

Space Complexity

O(N), where ‘N’ is the number of Houses in the city.

 

O(H) because we are using extra space to store addresses at each level and recursion stack space is used by the algorithm where ‘H’ is the height of the city. In the worst case (structure like skewed trees), ‘H’ will become ‘N’. Thus, the overall space complexity is O(N).

Code Solution
(100% EXP penalty)
Beautiful City
Full screen
Console