Leetcode: Minimum Height Trees

For a undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height are called minimum height trees (MHTs). Given such a graph, write a function to find all the MHTs and return a list of their root labels.

Format
The graph contains n nodes which are labeled from 0 to n - 1. You will be given the number n and a list of undirected edges (each edge is a pair of labels).

You can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges.

Example 1:

Given n = 4, edges = [[1, 0], [1, 2], [1, 3]]

        0
        |
        1
       / 
      2   3
return [1]

Example 2:

Given n = 6, edges = [[0, 3], [1, 3], [2, 3], [4, 3], [5, 4]]

     0  1  2
       | /
        3
        |
        4
        |
        5
return [3, 4]

Hint:

  1. How many MHTs can a graph have at most?  Answer: 1 or 2

build graph first, also buil indegree array, then find leaf and remove them among their neighbors, level by level. Until left less 2 nodes

这道题跟一般Topological Sort不大一样在于它不是DAG,因此有代码37行,从neighbor的neighbor list当中删掉当前节点

 1 public class Solution {
 2     public List<Integer> findMinHeightTrees(int n, int[][] edges) {
 3         List<Integer> leaves = new ArrayList<Integer>();
 4         if (edges==null || edges.length==0) {
 5             leaves.add(n-1);
 6             return leaves;
 7         }
 8         HashMap<Integer, ArrayList<Integer>> graph = new HashMap<Integer, ArrayList<Integer>>();
 9         int[] indegree = new int[n];
10         
11         for (int i=0; i<n; i++) {
12             graph.put(i, new ArrayList<Integer>());
13         }
14         
15         //build the graph
16         for (int[] edge : edges) {
17             graph.get(edge[0]).add(edge[1]);
18             graph.get(edge[1]).add(edge[0]);
19             indegree[edge[0]]++;
20             indegree[edge[1]]++;
21         }
22         
23         //find the leaves
24         for (int i=0; i<n; i++) {
25             if (indegree[i] == 1) {
26                 leaves.add(i);
27             }
28         }
29         
30         //topological sort until n<=2
31         while (n > 2) {
32             List<Integer> newLeaf = new ArrayList<Integer>();
33             for (Integer leaf : leaves) {
34                 List<Integer> neighbors = graph.get(leaf);
35                 for (Integer neighbor : neighbors) {
36                     indegree[neighbor]--;
37                     graph.get(neighbor).remove(leaf);
38                     if (indegree[neighbor] == 1) 
39                         newLeaf.add(neighbor);
40                 }
41                 //delete leaf from graph
42                 n--;
43             }
44             leaves = newLeaf;
45         }
46         
47         return leaves;
48     }
49 }
原文地址:https://www.cnblogs.com/EdwardLiu/p/5092926.html