Finding Minimum Height Trees via Topological Sorting
Create an adjacency list to represent the graph.
Initialize a list to keep track of the current leaf nodes.
Populate the initial list of leaf nodes (nodes with only one connection).
Use a loop to trim the leaf nodes until there are at most two nodes left.
In each iteration of the loop:
a. Remove the current leaf nodes from the graph.
b. Update the adjacency list by removing the edges connected to the leaf nodes.
c. Identify the new leaf nodes that emerge after trimming.
Once the loop is done, the remaining nodes are the centroids.
Return the centroids as the roots of the Minimum Height Trees.