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 = 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]

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.

一刷
思路是,用一個(gè)list記錄所有的node, list[i]為一個(gè)set,裝有所有與i相鄰點(diǎn)的編號(hào)。如果set的size為1,表示為葉子節(jié)點(diǎn),移去。即每次移去最外層(相同深度)的葉子。最終剩下來(lái)的為滿足條件的root

public class Solution {
    public List<Integer> findMinHeightTrees(int n, int[][] edges) {
        if(n == 1) return Collections.singletonList(0);
        List<Set<Integer>> adj = new ArrayList<>(n);
        
        for(int i=0; i<n; i++) adj.add(new HashSet<Integer>());
        for(int[] edge : edges){
            adj.get(edge[0]).add(edge[1]);
            adj.get(edge[1]).add(edge[0]);
        }
        
        List<Integer> leaves = new ArrayList<>();
        for(int i=0; i<n; i++)
            if(adj.get(i).size() == 1) leaves.add(i);
        
        while(n>2){
            n -= leaves.size();
            List<Integer> newLeaves = new ArrayList<>();
            for(int i:leaves){
                int j = adj.get(i).iterator().next();
                adj.get(j).remove(i);
                if(adj.get(j).size() == 1) newLeaves.add(j);
            }
            leaves = newLeaves;
        }
        return leaves;
    }
}

二刷
同上

class Solution {
    public List<Integer> findMinHeightTrees(int n, int[][] edges) {
        if(n == 1) return Collections.singletonList(0);
        List<Set<Integer>> adj = new ArrayList<>();
        for(int i=0; i<n; i++) adj.add(new HashSet<Integer>());
        for(int[] edge : edges){
            adj.get(edge[0]).add(edge[1]);
            adj.get(edge[1]).add(edge[0]);
        }
        
        List<Integer> leaves = new ArrayList<>();
        for(int i=0; i<n; i++){
            if(adj.get(i).size() == 1) leaves.add(i);
        }
        
        while(n>2){
            n -= leaves.size();
            List<Integer> newLeaves = new ArrayList<>();
            for(int i:leaves){
                int j = adj.get(i).iterator().next();
                adj.get(j).remove(i);
                if(adj.get(j).size() == 1) newLeaves.add(j);
            }
            leaves = newLeaves; 
        }
        return leaves;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問(wèn)題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,766評(píng)論 0 33
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,787評(píng)論 0 23
  • “噔、噔、噔” 女人踩著昏暗的燈 在雨巷 浮光掠影的水面 沉默地隱去
    Coral_23閱讀 180評(píng)論 0 0
  • 日精進(jìn)【打卡第143天】: 姓名:余成杰 公司:貞觀電器 盛和塾《六項(xiàng)精進(jìn)》224期學(xué)員 【知-學(xué)習(xí)】 《六項(xiàng)精進(jìn)...
    余成杰閱讀 91評(píng)論 0 0
  • 《桃花扇》是中國(guó)清代著名的傳奇劇本,作者是孔尚任,經(jīng)歷十余年三易其稿而成。作者將明末侯方域與秦淮艷姬李香君的悲歡離...
    妖妖z閱讀 1,095評(píng)論 3 13