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.
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 -- 找出无向图中以哪些节点为根,树的深度最小的更多相关文章
- [LeetCode] 310. Minimum Height Trees 解题思路
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- 【LeetCode】310. Minimum Height Trees 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 相似题目 参考资料 日期 题目地址:http ...
- 310. Minimum Height Trees
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- leetcode@ [310] Minimum Height Trees
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- [LeetCode] 310. Minimum Height Trees 最小高度树
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- lintcode:Find the Connected Component in the Undirected Graph 找出无向图汇总的相连要素
题目: 找出无向图汇总的相连要素 请找出无向图中相连要素的个数. 图中的每个节点包含其邻居的 1 个标签和 1 个列表.(一个无向图的相连节点(或节点)是一个子图,其中任意两个顶点通过路径相连,且不与 ...
- [LeetCode] Minimum Height Trees 最小高度树
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- [Swift]LeetCode310. 最小高度树 | Minimum Height Trees
For an undirected graph with tree characteristics, we can choose any node as the root. The result gr ...
- DFS应用——找出无向图的割点
[0]README 0.1) 本文总结于 数据结构与算法分析, 源代码均为原创, 旨在 理解 "DFS应用于找割点" 的idea 并用源代码加以实现: 0.2) 必须要事先 做个s ...
随机推荐
- C++ Websites
C++ Websites C++ 推荐网站 1.cprogramming.com 2.cppreference.com 3.cplusplus.com 4.Boost C++ Library
- Modulo operation
Modulo operation - Wikipedia https://en.wikipedia.org/wiki/Modulo_operation https://baike.baidu.com/ ...
- 【mlflow】mlflow打包、启动、换用mysql backend、mysql配置
mlflow是一个自动化机器学习平台,支持python2也支持python3 mlflow9.0添加了数据库作为tracking data的存储: https://github.com/mlflow/ ...
- JavaScript高级程序设计第三版学习笔记(一)之数据类型区分详谈
null.NaN.undefined三者的区别是什么? 在初次接触到JavaScript的时候,傻傻的分不清null.NaN.undefined三者到底区别何在,在实际的项目开发中也因为这个问题而困惑 ...
- openstack配置域名访问
#openstack配置域名访问 openstack pike 安装 目录汇总 http://www.cnblogs.com/elvi/p/7613861.html #主要是在默认配置的基础上,做了个 ...
- cocos2d 特效
一.特效概念 特效是让精灵(CCSprite)执行某种特殊的效果.其实,特效也是一种动画! 但是,为什么要把特效与动画区分呢?因为,特效是基于网格属性来进行的. 如何区分动画与特效?简单的将,当使用到 ...
- 数据展现-百度js绘图
echarts:酷炫的绘图效果 http://echarts.baidu.com/examples/#chart-type-calendar
- 查看MySQL锁定情况
SHOW STATUS LIKE '%Table_locks%' Table_locks_immediate | 105 | Table_locks_waited | 3 ...
- java-mybaits-009-mybatis-spring-使用,SqlSessionFactoryBean、事务
一.版本限制 参看地址:http://www.mybatis.org/spring/ 二.使用入门 2.1.pom <dependency> <groupId>org.myba ...
- 1107 Social Clusters[并查集][难]
1107 Social Clusters(30 分) When register on a social network, you are always asked to specify your h ...