码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode: Minimum Height Trees

时间:2016-01-01 07:21:47      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

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

 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                 indegree[leaf]--;
43                 neighbors.clear();
44                 n--;
45             }
46             leaves = newLeaf;
47         }
48         
49         return leaves;
50     }
51 }

 

Leetcode: Minimum Height Trees

标签:

原文地址:http://www.cnblogs.com/EdwardLiu/p/5092926.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!