Party in Town
Submissions: 267   Accuracy:

21.36%

  Difficulty: Medium   Marks: 4

There is a Town which has 'N' Houses numbered from 1 to N, connected by 'N-1' bi directional roads which forms a tree structure. The people of the town are addicted to Partying.
For a party, they choose a house, from which the distance to the farthest house is the minimum.
You need to tell what is this minimum distance which we can achieve by choosing a house to party optimally.

 

Input:
The first line contains an integer 'T' - the number of testcases.
The first line of each testcase contains one integer N- the number of House.
Then each of the next N-1 lines contains two integers h1 and h2 meaning that house h1 and h2 is connected by a road. 

 

Output:
For each of the testcase , Output an integer denoting the minimum distance to the farthest house from the House choosen to Party.

 

Constraints:
1<=T<=10
1<=N<=1000
1<=h1,h2<=N

 

Example:
Input:
1
4
1 2
2 3
2 4
Output:
1

 

EXPLANATION:
If we choose the house number 2 to Party, Then we will have the maximum distance as 1 to the farthest nodes.

 

** For More Input/Output Examples Use 'Expected Output' option **

Author: iamabjain


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.