Last Updated: 25 Jan, 2021

Beautiful City

Easy
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.
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.

Approaches

01 Approach

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

 

02 Approach

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.

 

  • Create a queue and enqueue the address of the given 'firstHouse' in it.
  • Initialize an integer variable ‘houseCount’: = 1, it will represent the number of houses in the current level of the city.
  • Run a while loop till queue not become empty and in each iteration perform following steps
    • Create a pointer ‘previous’ and initialize it by NULL.
    • Run a loop while loop till ‘houseCount’ is greater than 0. And in each iteration of this while loop do following-:
      • If ‘previous’ is not NULL. then assign the address of the front house of the queue to the ‘next’ pointer of the house represented by the ‘previous’ pointer.
      • Assign address of the front house of the queue to the ‘previous’ pointer.
      • Dequeue the house at the front of the queue, and enqueue its left and right child (if exist).
      • Decrement ‘houseCount’ by 1.
    • The queue will now have all the houses of the next level. Assign size of the queue to the variable ‘houseCount’.

03 Approach

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 -:

 

  • Create a pointer ‘firstHouseAddress’ and initialize it by the address of the given first house at level ‘0’. The pointer ‘firstHouseAddress’ will keep the address of the first node of the current level.
  • We run a while loop till ‘firstHouseAddress’ is not equal to NULL, and in each iteration do the following -:
    • Create a pointer ‘temp’ and assign the value of ‘firstHouseAddress’ to it, we will use this pointer ‘temp’ to iterate over houses of the current level.
    • Assign the address of the first node of next level to ‘firstHouseAddress’ , if there is no next level then assign NULL to it.
    • Iterate over the current level using the pointer ‘temp’, the next level is formed by the left or right house of the houses of the current level, so while iterating you can keep track of houses encountered on the next level from left to right and connect them accordingly.
  • All the next pointers will be populated after this while loop.