Roots of the tree having minimum height

Moderate
0/80
Average time to solve is 25m
profile
Contributed by
38 upvotes
Asked in companies
OYOUberAmazon

Problem statement

You are given an undirected Tree having 'N' nodes. The nodes are numbered from 1 to 'N'. Your task is to find the sorted list of all such nodes, such that rooting the tree on that node gives the minimum height possible of the given tree.

A tree is a connected acyclic graph. The height of a rooted tree is the maximum value of the distance between the root and all nodes of the tree.

Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of the input contains an integer, 'T,’ denoting the number of test cases.

The first line of each test case contains an integer, 'N,’ denoting the number of nodes.

The next ‘N’-1 lines of each test case contain two space-separated integers, ‘X’ and ‘Y’ separated by a single space, ‘X’ and ’Y’ denote the two nodes connected by an undirected edge.
Output Format:
For each test case, return the list of all such nodes such that rooting the tree on that node gives the minimum height possible. 

Return all the nodes in a sorted manner.
Note :
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 10^5 
1 <= X,Y <= N

The input edges form a tree.

Time limit: 1 sec
Sample Input 1:
2
3
1 2 
2 3
4
1 2
3 1
4 1
Sample Output 1:
2
1
Explanation for Sample Input 1:
For the first test case, if we root the tree at Node 2, the height of the resultant tree is 1 which is the minimum possible height. 
For the second test case, if we root the tree at Node 1, the height of the resultant tree is 1 which is the minimum possible height. 
Sample Input 2:
2
4
1 3 
2 3
4 2
2 
1 2
Sample Output 2:
2 3
1 2
Hint

Select each node as the root of the tree, and find the height of each tree.

Approaches (2)
Brute Force Approach

The idea is to iterate through all the nodes one by one, select that node as the root of the tree and find the height of the formed tree. We will also maintain a list of nodes that gives the minimum height. Whenever we find a node that gives a minimum height, we will clear our list and that node to the list and update the minimum height that we have found till now. If a node gives the same height as the minimum height, then we will add that node to our output list. Note that we will ignore all nodes that give greater height than minimum height. To find the height of a rooted tree we can use Depth First Search (DFS) or Breadth First Search (BFS).

 

Steps: 

  1. Let 'MIN_HEIGHT' be a variable that stores the minimum height of the tree which we can get. Initialize it as INT_MAX.
  2. Let 'ROOT_LIST' be a list of nodes that gives the minimum height.
  3. Iterate through ‘i’ = 1 to ‘N’ 
    • Let 'HEIGHT' be the height of the tree rooted at Node i.
    • If 'MIN_HEIGHT' is greater than 'HEIGHT', then
      • Set 'MIN_HEIGHT' as 'HEIGHT'.
      • Clear the 'ROOT_LIST' array
      • Add ‘i’ to the 'ROOT_LIST' array.
    • Otherwise, if 'HEIGHT' is equal to 'MIN_HEIGHT', then
      • Add ‘i’ to the 'ROOT_LIST' array.
  4. Return the array 'ROOT_LIST'.
Time Complexity

O(N^2), where ‘N’ is the number of nodes.

 

It takes O(N) time to find the height of a rooted tree, and we need to find the height for each of the ‘N’ nodes of the tree. Hence, the overall Time Complexity is O(N^2).

Space Complexity

O(N), where ‘N’ is the number of nodes in the tree.

 

The extra space required to find height of a tree is proportional to the number of nodes in the tree. Hence, the overall Space Complexity is O(N).

Code Solution
(100% EXP penalty)
Roots of the tree having minimum height
Full screen
Console