[Swift]LeetCode310. 最小高度樹 | Minimum Height Trees

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-bwqehtab-kq.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

For an 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.node

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).git

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.github

Example 1 :微信

Input: , 

        0
        |
        1
       / \
      2   3 

Output: 
n = 4edges = [[1, 0], [1, 2], [1, 3]][1]

Example 2 :app

Input: , 

     0  1  2
      \ | /
        3
        |
        4
        |
        5 

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

Note:函數

  • 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.」
  • The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf.

對於一個具備樹特徵的無向圖,咱們可選擇任何一個節點做爲根。圖所以能夠成爲樹,在全部可能的樹中,具備最小高度的樹被稱爲最小高度樹。給出這樣的一個圖,寫出一個函數找到全部的最小高度樹並返回他們的根節點。spa

格式code

該圖包含 n 個節點,標記爲 0 到 n - 1。給定數字 n 和一個無向邊 edges 列表(每個邊都是一對標籤)。orm

你能夠假設沒有重複的邊會出如今 edges 中。因爲全部的邊都是無向邊, [0, 1]和 [1, 0] 是相同的,所以不會同時出如今 edges 裏。

示例 1:

輸入: , 

        0
        |
        1
       / \
      2   3 

輸出: 
n = 4edges = [[1, 0], [1, 2], [1, 3]][1]

示例 2:

輸入: , 

     0  1  2
      \ | /
        3
        |
        4
        |
        5 

輸出: n = 6edges = [[0, 3], [1, 3], [2, 3], [4, 3], [5, 4]][3, 4]

說明:

  •  根據樹的定義,樹是一個無向圖,其中任何兩個頂點只經過一條路徑鏈接。 換句話說,一個任何沒有簡單環路的連通圖都是一棵樹。
  • 樹的高度是指根節點和葉子節點之間最長向下路徑上邊的數量。

1724 ms

 1 class Solution {
 2     func findMinHeightTrees(_ n: Int, _ edges: [[Int]]) -> [Int]{
 3         var graph = [Int : Set<Int>]()
 4         for edge in edges {
 5             if graph[edge[0]] == nil {
 6                 graph[edge[0]] = [edge[1]]
 7             }else {
 8                 graph[edge[0]]!.insert(edge[1])
 9             }
10             if graph[edge[1]] == nil {
11                 graph[edge[1]] = [edge[0]]
12             }else {
13                 graph[edge[1]]!.insert(edge[0])
14             }
15             
16         }
17         while graph.count > 2 {
18             let list = graph.filter{$1.count == 1}
19             for dict in list {
20                 graph[dict.value.first!]?.remove(dict.key)
21                 graph[dict.key] = nil
22             }
23         }
24         
25         let res = Array(graph.keys)
26         if res.isEmpty {
27             return [0]
28         }
29         return res
30     }
31 }
相關文章
相關標籤/搜索