Leetcode 310: Minimum Height Trees

Posted Keep walking

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 310: 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 = 4edges = [[1, 0], [1, 2], [1, 3]]

        0
        |
        1
       /       2   3

return [1]

Example 2:

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

     0  1  2
      \ | /
        3
        |
        4
        |
        5

return [3, 4]

Note:

(1) According to the definition of tree on Wikipedia: “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.”

(2) The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf.

 

  1 public class Solution {
  2     public IList<int> FindMinHeightTrees(int n, int[,] edges) {
  3         if (n == 1) return new List<int>() {0};
  4         
  5         var dict = new Dictionary<int, IList<int>>();
  6         
  7         for (int i = 0; i < edges.GetLength(0); i++)
  8         {
  9             if (!dict.ContainsKey(edges[i, 0]))
 10             {
 11                 dict[edges[i, 0]] = new List<int>();
 12             }
 13             
 14             dict[edges[i, 0]].Add(edges[i, 1]);
 15             
 16             if (!dict.ContainsKey(edges[i, 1]))
 17             {
 18                 dict[edges[i, 1]] = new List<int>();
 19             }
 20             
 21             dict[edges[i, 1]].Add(edges[i, 0]);
 22         }
 23         
 24         var leaves = new List<int>();
 25         
 26         for (int i = 0; i < n; i++)
 27         {
 28             if (dict[i].Count == 1)
 29             {
 30                 leaves.Add(i);
 31             }
 32         }
 33         
 34         while (n > 2)
 35         {
 36             n -= leaves.Count;
 37             var newLeaves = new List<int>();
 38             
 39             foreach (var l in leaves)
 40             {
 41                 dict[dict[l][0]].Remove(l);
 42                 
 43                 if (dict[dict[l][0]].Count == 1) 
 44                 {
 45                     newLeaves.Add(dict[l][0]);
 46                 }
 47             }
 48             
 49             leaves = newLeaves;
 50         }
 51         
 52         return leaves;
 53     }
 54 }
 55 
 56 public class Solution1 {
 57     public IList<int> FindMinHeightTrees(int n, int[,] edges) {
 58         var dict = new Dictionary<int, IList<int>>();
 59         
 60         for (int i = 0; i < edges.GetLength(0); i++)
 61         {
 62             if (!dict.ContainsKey(edges[i, 0]))
 63             {
 64                 dict[edges[i, 0]] = new List<int>();
 65             }
 66             
 67             dict[edges[i, 0]].Add(edges[i, 1]);
 68             
 69             if (!dict.ContainsKey(edges[i, 1]))
 70             {
 71                 dict[edges[i, 1]] = new List<int>();
 72             }
 73             
 74             dict[edges[i, 1]].Add(edges[i, 0]);
 75         }
 76         
 77         int curMin = n;
 78         var curResult = new List<int>();
 79         
 80         for (int i = 0; i < n; i++)
 81         {
 82             var h = new int[1] {Int32.MinValue};
 83             DFS(i, dict, new bool[n], 0, h);
 84             
 85             if (h[0] < curMin)
 86             {
 87                 curMin = h[0];
 88                 curResult = new List<int>() {i};
 89             }
 90             else if (h[0] == curMin)
 91             {
 92                 curResult.Add(i);
 93             }
 94         }
 95         
 96         return curResult;
 97     }
 98     
 99     private void DFS(int n, Dictionary<int, IList<int>> dict, bool[] visited, int cur, int[] result)
100     {
101         if (visited[n]) return;
102         
103         visited[n] = true;
104         
105         result[0] = Math.Max(result[0], cur);
106         
107         if (dict.ContainsKey(n))
108         {
109             foreach (var c in dict[n])
110             {
111                 DFS(c, dict, visited, cur + 1, result);
112             }
113         }
114     }
115 }

 


以上是关于Leetcode 310: Minimum Height Trees的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 310: Minimum Height Trees

310. Minimum Height Trees

310. Minimum Height Trees

Leetcode: Minimum Domino Rotations For Equal Row

310. Minimum Height Trees

310. Minimum Height Trees