New update is available. Click here to update.

Roots of the tree having minimum height

Posted: 13 Mar, 2021
Difficulty: Moderate

PROBLEM STATEMENT

Try Problem

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.

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