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]

Show Hint

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.

 
 
vector<int> findMinHeightTrees(int n, vector<pair<int, int>>& edges) {
//corner case
if ( n <= ) return {}; //construct a edges search data stucture
vector<unordered_set<int>> graph(n);
for (auto e : edges) {
graph[e.first].insert(e.second);
graph[e.second].insert(e.first);
} //find all of leaf nodes
vector<int> current;
for (int i=; i<graph.size(); i++){
if (graph[i].size() == ) current.push_back(i);
} // BFS the graph
while (true) {
vector<int> next;
for (int node : current) {
for (int neighbor : graph[node]) {
graph[neighbor].erase(node);
if (graph[neighbor].size() == ) next.push_back(neighbor);
}
}
if (next.empty()) break;
current = next;
}
return current;
}

每次去掉最外面一圈叶子节点。最后剩下的节点集合即为所求。

310. Minimum Height Trees -- 找出无向图中以哪些节点为根,树的深度最小的更多相关文章

  1. [LeetCode] 310. Minimum Height Trees 解题思路

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  2. 【LeetCode】310. Minimum Height Trees 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 相似题目 参考资料 日期 题目地址:http ...

  3. 310. Minimum Height Trees

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  4. leetcode@ [310] Minimum Height Trees

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  5. [LeetCode] 310. Minimum Height Trees 最小高度树

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  6. lintcode:Find the Connected Component in the Undirected Graph 找出无向图汇总的相连要素

    题目: 找出无向图汇总的相连要素 请找出无向图中相连要素的个数. 图中的每个节点包含其邻居的 1 个标签和 1 个列表.(一个无向图的相连节点(或节点)是一个子图,其中任意两个顶点通过路径相连,且不与 ...

  7. [LeetCode] Minimum Height Trees 最小高度树

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

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

    For an undirected graph with tree characteristics, we can choose any node as the root. The result gr ...

  9. DFS应用——找出无向图的割点

    [0]README 0.1) 本文总结于 数据结构与算法分析, 源代码均为原创, 旨在 理解 "DFS应用于找割点" 的idea 并用源代码加以实现: 0.2) 必须要事先 做个s ...

随机推荐

  1. Oracle HA 之 SERVICE和DRM实战

    第一部分:service实战 --oracle 11gR2中创建service的方法:db console和srvctl两种方法. --db console创建service方法-略 --srvctl ...

  2. CMDB内功心法,助我登上运维之巅

    很多70.80后甚至90后都在金庸.古龙.梁羽生先生等武林大家熏陶下成长的,这么多年过去了,我的武侠梦依然不曾散去.曾几何,梦想有一天练就一身绝学,搂着小师妹花前月下,仗剑走天涯,快意人生.可每次的酣 ...

  3. Day19 客户关系系统实战

    day19 今日内容 Service事务 客户关系管理系统     Service事务 在Service中使用ThreadLocal来完成事务,为将来学习Spring事务打基础! 1 DAO中的事务 ...

  4. Day05 xml详解

    day05总结 今日内容 XML语法 XML约束之DTD XML解析器介绍 XML解析之JAXP( DOM.SAX ) DOM4J Schema   一.XML语法 XML概述   1 什么是XML ...

  5. android 本地通知

    NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notificat ...

  6. samba文件共享服务配置一(共2节)

    一.samba服务简介 Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Server Messages Block,信息服务块)是一种在局域网上 ...

  7. java基础语法 List

    List:元素是有序的(怎么存的就怎么取出来,顺序不会乱),元素可以重复(角标1上有个3,角标2上也可以有个3)因为该集合体系有索引, ArrayList:底层的数据结构使用的是数组结构(数组长度是可 ...

  8. 2.4 The Object Model -- Computed Properties and Aggregate Data with @each(计算的属性和使用@each聚合数据)

    1. 通常,你可能有一个计算的属性依赖于数组中的所有元素来确定它的值.例如,你可能想要计算controller中所有todo items的数量,以此来确定完成了多少任务. export default ...

  9. P1757 通天之分组背包 / hdu1712 ACboy needs your help (分组背包入门)

    P1757 通天之分组背包 hdu1712 ACboy needs your help hdu1712题意:A[i][j]表示用j天学习第i个课程能够得到A[i][j]的收益,求m天内获得的收益最大值 ...

  10. Java实现获取属性文件的参数值

    Java实现获取属性文件的参数值 1,属性文件内容(analysis.properties),路径必须在:src根目录下: #client data path analysis.client.data ...