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.
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.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10 1 <= N <= 10^5 1 <= X,Y <= N The input edges form a tree. Time limit: 1 sec
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).