摘要:現(xiàn)在要求在這樣一棵生成樹中,找到生成樹的高度最低的所有根節(jié)點。然后利用鄰接表的相關(guān)屬性來判斷當(dāng)前節(jié)點是否是葉節(jié)點。度數(shù)為一的頂點就是葉節(jié)點。這里使用異或的原因是對同一個值進行兩次異或即可以回到最初值。
題目
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. 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 : Input: n = 4, edges = [[1, 0], [1, 2], [1, 3]] 0 | 1 / 2 3 Output: [1] Example 2 : Input: n = 6, edges = [[0, 3], [1, 3], [2, 3], [4, 3], [5, 4]] 0 1 2 | / 3 | 4 | 5 Output: [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.
在無向圖的生成樹中,我們可以指定任何一個節(jié)點為這棵樹的根節(jié)點。現(xiàn)在要求在這樣一棵生成樹中,找到生成樹的高度最低的所有根節(jié)點。
其實,決定一棵樹的高度往往是樹中的最長路徑,只有我們選擇最長路徑的中間點才能夠盡可能減少樹的高度。那么我們?nèi)绾握业剿械闹虚g節(jié)點呢?
當(dāng)出發(fā)點只有兩個時,我們知道中間節(jié)點就是從出發(fā)點同時出發(fā),當(dāng)二者相遇或者二者只間隔一個位置是所在的點就是兩個出發(fā)點的重點。那么多個出發(fā)點也是同樣的道理,每個人從各個出發(fā)點出發(fā),最終相遇的點就是我們所要找的中間點。
這題的思路有些類似于拓?fù)渑判颍看挝覀兌紩コ腥攵葹?的點,因為這些點肯定是葉節(jié)點。然后不停的往中間走,直到剩下最后一個葉節(jié)點或是最后兩個葉節(jié)點。
0 | 1 / 2 3
這個圖中刪除所有入度為0的點就只剩下1,因此我們知道1一定就是我們所要求的根節(jié)點
思路一:圖論這一種解法著重強調(diào)了利用圖論中的數(shù)據(jù)結(jié)構(gòu)來解決問題。這里我們采用圖論中的鄰接表來存儲圖中的點和邊。然后利用鄰接表的相關(guān)屬性來判斷當(dāng)前節(jié)點是否是葉節(jié)點。
public List思路二:簡化數(shù)據(jù)結(jié)構(gòu)findMinHeightTrees(int n, int[][] edges) { if(n==1) return Collections.singletonList(0); //初始化鄰接表 List > adj = new ArrayList >(); for(int i = 0 ; i ()); } for(int[] edge : edges) { adj.get(edge[0]).add(edge[1]); adj.get(edge[1]).add(edge[0]); } List leaves = new ArrayList (); for(int i = 0 ; i 2) { n -= leaves.size(); List 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; }
這里使用degree數(shù)組存儲每個頂點的度數(shù),即連接的變數(shù)。度數(shù)為一的頂點就是葉節(jié)點。再用connected存儲每個頂點所連接的所有邊的異或值。這里使用異或的原因是對同一個值進行兩次異或即可以回到最初值。
public ListfindMinHeightTrees2(int n, int[][] edges) { if(n==1) return Collections.singletonList(0); int[] connected = new int[n]; int[] degree = new int[n]; for(int[] edge : edges) { int v1 = edge[0]; int v2 = edge[1]; connected[v1] ^= v2; connected[v2] ^= v1; degree[v1]++; degree[v2]++; } LinkedList queue = new LinkedList (); for(int i = 0 ; i 2 && !queue.isEmpty()) { int size = queue.size(); for(int i = 0 ; i result = new ArrayList (); result.addAll(queue); return result; }
想要了解更多開發(fā)技術(shù),面試教程以及互聯(lián)網(wǎng)公司內(nèi)推,歡迎關(guān)注我的微信公眾號!將會不定期的發(fā)放福利哦~
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/72303.html
摘要:可以從頭邊同時進行,查看葉子節(jié)點并加入到葉子節(jié)點鏈表遍歷一遍后,葉子節(jié)點鏈表為。將葉子節(jié)點保存下來。這個時候就會有第二層葉子節(jié)點那些在列表當(dāng)中為的點,用同樣的方法進行剝除。最后留在葉子節(jié)點里面的點即可以為根。 題目: For a undirected graph with tree characteristics, we can choose any node as the root...
摘要:題目鏈接圖的題,和差不多。來解,每次放入只有一個的現(xiàn)在的。然后直到只剩最上面一層。注意考慮單獨的和別的不相連。比如這種情況,就有三個點都不和別的相連。還有的時候就要返回。 Minimum Height Trees 題目鏈接:https://leetcode.com/problems... 圖的題,和course schedule差不多。bfs來解,每次放入只有一個edge的node(現(xiàn)...
Problem You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-negative 2D map, in this map: 0 represents the obstacle cant be reached.1 represents the ground ...
Problem Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required. Example 1: Input: [[0, 30],[5,...
Unique Binary Search Trees Problem Given n, how many structurally unique BSTs (binary search trees) that store values 1...n? Example Given n = 3, there are a total of 5 unique BSTs. 1 3 3...
閱讀 3225·2021-11-08 13:21
閱讀 1202·2021-08-12 13:28
閱讀 1413·2019-08-30 14:23
閱讀 1935·2019-08-30 11:09
閱讀 850·2019-08-29 13:22
閱讀 2694·2019-08-29 13:12
閱讀 2557·2019-08-26 17:04
閱讀 2265·2019-08-26 13:22