310. Minimum Height Trees

p.p1
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]


Code

public class Solution {
    public List<Integer> findMinHeightTrees(int n, int[][] edges) {
        if (n == 0) return new ArrayList<>();
        if (n == 1) {
            List<Integer> list = new ArrayList<>();
            list.add(0);
            return list;
        }
        
        List<Integer>[] degrees = new ArrayList[n];
        
        for (int i = 0; i < degrees.length; i++) {
            degrees[i] = new ArrayList<>();
        }
        
        for (int i = 0; i < edges.length; i++) {
            int a = edges[i][0], b = edges[i][1];
            degrees[a].add(b);
            degrees[b].add(a);
        }
        
        List<Integer> leaves = new ArrayList<>();
        for (int i = 0; i < degrees.length; i++) {
            if (degrees[i].size() == 1) leaves.add(i);
        }
        
        int remain = n;
        while (remain > 2) {
            int size = leaves.size();
            remain -= size;
            
            List<Integer> newLeaves = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                int leaf = leaves.get(i);
                for (int j = 0; j < degrees[leaf].size(); j++) {
                    int remove = degrees[leaf].get(j);
                    degrees[remove].remove(Integer.valueOf(leaf));
                    if (degrees[remove].size() == 1) newLeaves.add(remove);
                }
            }
            leaves = newLeaves;
        }
        
        return leaves;
    }
}

Solution

找到所有入度為1的節(jié)點(diǎn)硝枉,這些節(jié)點(diǎn)都是葉子節(jié)點(diǎn)。通過BFS移除這些節(jié)點(diǎn)。剩余所有節(jié)點(diǎn)中入度為1的是新的葉子節(jié)點(diǎn)。循環(huán)直至剩余2個(gè)以下的節(jié)點(diǎn)為止。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子纺非,更是在濱河造成了極大的恐慌,老刑警劉巖赘方,帶你破解...
    沈念sama閱讀 219,539評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件烧颖,死亡現(xiàn)場離奇詭異,居然都是意外死亡窄陡,警方通過查閱死者的電腦和手機(jī)炕淮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評論 3 396
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來跳夭,“玉大人涂圆,你說我怎么就攤上這事”姨荆” “怎么了乘综?”我有些...
    開封第一講書人閱讀 165,871評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長套硼。 經(jīng)常有香客問我,道長胞皱,這世上最難降的妖魔是什么邪意? 我笑而不...
    開封第一講書人閱讀 58,963評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮反砌,結(jié)果婚禮上雾鬼,老公的妹妹穿的比我還像新娘。我一直安慰自己宴树,他們只是感情好策菜,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,984評論 6 393
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著酒贬,像睡著了一般又憨。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上锭吨,一...
    開封第一講書人閱讀 51,763評論 1 307
  • 那天蠢莺,我揣著相機(jī)與錄音,去河邊找鬼零如。 笑死躏将,一個(gè)胖子當(dāng)著我的面吹牛锄弱,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播祸憋,決...
    沈念sama閱讀 40,468評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼会宪,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蚯窥?” 一聲冷哼從身側(cè)響起掸鹅,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎沟沙,沒想到半個(gè)月后河劝,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,850評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡矛紫,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,002評論 3 338
  • 正文 我和宋清朗相戀三年赎瞎,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片颊咬。...
    茶點(diǎn)故事閱讀 40,144評論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡务甥,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出喳篇,到底是詐尸還是另有隱情敞临,我是刑警寧澤,帶...
    沈念sama閱讀 35,823評論 5 346
  • 正文 年R本政府宣布麸澜,位于F島的核電站挺尿,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏炊邦。R本人自食惡果不足惜编矾,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,483評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望馁害。 院中可真熱鬧窄俏,春花似錦、人聲如沸碘菜。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,026評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽忍啸。三九已至仰坦,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間吊骤,已是汗流浹背缎岗。 一陣腳步聲響...
    開封第一講書人閱讀 33,150評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留白粉,地道東北人传泊。 一個(gè)月前我還...
    沈念sama閱讀 48,415評論 3 373
  • 正文 我出身青樓鼠渺,卻偏偏與公主長得像,于是被迫代替她去往敵國和親眷细。 傳聞我的和親對象是個(gè)殘疾皇子拦盹,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,092評論 2 355

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