The best hub in a tree
A company has n servers, numbered 0 to n - 1, joined by the n - 1 two-way links in edges. Every server reaches every other one by exactly one route, so the network is a tree.
You will pick one server as the hub. The hub's height is the largest number of links on the route from the hub to any other server. Return every server whose height is as small as possible, in any order.
n = 7, edges = [[0, 1], [1, 2], [1, 3], [3, 4], [4, 5], [4, 6]]Output[3]From server 3, every server is at most 2 links away. Servers 1 and 4 are each 3 links from the far side, and every other server is farther still.
n = 6, edges = [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5]]Output[2, 3]The servers form a line of 6. Servers 2 and 3 both have height 3, and no server does better.
n = 1, edges = []Output[0]A single server is the only choice, with height 0.
1 ≤ n ≤ 2 × 104
len(edges) == n - 1
Each link is [a, b] with 0 ≤ a, b < n and a ≠ b.
The links join all n servers into one tree: no link is listed twice, and there is no loop.
Plan it first
Write a line for each before you code, then say them out loud. Compare with the Approach tab afterwards.