Imagine you're a city planner tasked with placing a central communication tower in a connected network of cities. The cities are connected by roads forming a tree structure - meaning every city can reach every other city through exactly one path, with no circular routes.
Given n cities labeled from 0 to n-1 and an array of n-1 roads where edges[i] = [a, b] represents a direct road between cities a and b, you need to find the optimal locations for your communication tower.
The height of your communication network is the maximum number of road hops needed to reach the farthest city from your tower location. Your goal is to minimize this maximum distance - find all possible tower locations that result in the smallest possible network height.
Return a list of all optimal tower locations (city labels) that minimize the communication network height.
Input & Output
Visualization
Time & Space Complexity
Each node is processed exactly once, each edge is examined at most twice
Space for adjacency list, degree array, and queue
Constraints
- 1 โค n โค 2 ร 104
- edges.length == n - 1
- 0 โค ai, bi < n
- ai โ bi
- The given input is guaranteed to be a tree