Minimum Height Trees
Given a tree of n nodes labeled 0..n-1 described by n-1 undirected edges, a node can be chosen as root, giving a rooted tree of some height. Return the labels of all roots that minimize the tree's height (there are at most two such roots).
Open official problem prompt ↗Find the tree's center(s): the node or pair of nodes whose greatest distance to any leaf is as small as possible.
Peeling an onion from the outside in; the last layer you cannot peel without dropping below two nodes is the core.
- Input
- n = 4, edges = [[1, 0], [1, 2], [1, 3]]
- Output
- [1]
- Why
- Rooting at the central node 1 gives height 1; any leaf root gives height 2.
1 <= n <= 2 * 10^4edges.length == n - 10 <= a_i, b_i < na_i != b_iThe given edges form a tree (connected, acyclic)