A tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree.
Given a tree of n
nodes labelled from 0
to n - 1
, and an array of n - 1
edges
where edges[i] = [ai, bi]
indicates that there is an undirected edge between the two nodes ai
and bi
in the tree, you can choose any node of the tree as the root. When you select a node x
as the root, the result tree has height h
. Among all possible rooted trees, those with minimum height (i.e. min(h)
) are called minimum height trees (MHTs).
Return a list of all MHTs' root labels. You can return the answer in any order.
The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf.
Example 1:
Input: n = 4, edges = [[1,0],[1,2],[1,3]] Output: [1] Explanation: As shown, the height of the tree is 1 when the root is the node with label 1 which is the only MHT.
Example 2:
Input: n = 6, edges = [[3,0],[3,1],[3,2],[3,4],[5,4]] Output: [3,4]
Constraints:
1 <= n <= 2 * 104
edges.length == n - 1
0 <= ai, bi < n
ai != bi
(ai, bi)
are distinct.This problem aims to find all root nodes that result in minimum height trees. The solution leverages the concept of topological sorting to efficiently achieve this.
Core Idea:
The algorithm works by iteratively removing leaf nodes (nodes with only one neighbor) from the tree. This process continues until either only one or two nodes remain. The remaining nodes are the roots of the minimum height trees. This is because removing leaf nodes progressively moves towards the center of the tree.
Algorithm Steps:
Handle Trivial Case: If the number of nodes (n) is 1, return [0] as the only node is the root.
Build Adjacency List and Degree Array: Create an adjacency list (g
) to represent the tree's structure and a degree
array to store the degree (number of neighbors) of each node.
Initialize Queue: Add all leaf nodes to a queue (q
).
Iterative Removal: While the queue is not empty, perform the following:
ans
to store the nodes to be removed in this iteration.ans
.ans
list to hold the potential root nodes found in that round.Return Result: After the loop finishes, ans
contains the root nodes of the minimum height trees.
Time and Space Complexity:
Code Examples (Python, Java, C++, Go, TypeScript):
The provided code examples in various languages effectively implement this algorithm. They all follow the same steps described above. Note the minor variations in syntax and data structures (lists, vectors, maps, etc.) depending on the specific language. The use of a deque (double-ended queue) in some solutions can further optimize queue operations.
Example Walkthrough (Python):
Let's trace the Python code for the input n = 6, edges = [[3,0],[3,1],[3,2],[3,4],[5,4]]
:
g
will be an adjacency list representing the tree connections.degree
will store degrees: [1, 1, 1, 4, 2, 1]
initially.q
.q
.ans
will contain [3, 4]
, which are the centers of the minimum height trees.This topological sorting approach provides an efficient and elegant way to solve the Minimum Height Trees problem. The iterative removal of leaf nodes guarantees that the algorithm converges to the centers of the tree, giving us the desired root nodes.