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 ...
随机推荐
- SUBSTRING_INDEX()
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_substring-index ) DEFAULT 'fru ...
- 介绍一个Redis的WEB 客户端
http://webd.is/ $ git clone git://github.com/nicolasff/webdis.git $ cd webdis$ make$ ...
- wireshark, loopback
swapondd if=/dev/zero of=/data/mnt/swap bs=1024 count=8024000 sudo apt-get install wireshark sudo gr ...
- Python爬虫基础(四)Requests库的使用
requests文档 首先需要安装:pip install requests get请求 最基本的get: # -*- coding: utf-8 -*-import requests respons ...
- Bungee Jumping---hdu1155(物理题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1155 题目很长,但是很容易理解,就是人从高s的桥上跳下来,手拉着长为l的绳子末端,如果绳子太短那么人将 ...
- Python开发【笔记】:探索Python F-strings
F-strings 在python3.6.2版本中,PEP 498 提出一种新型字符串格式化机制,被称为“字符串插值”或者更常见的一种称呼是F-strings(主要因为这种字符串的第一个字母是f) 简 ...
- sql server学习路径地址
联机丛书2005:https://docs.microsoft.com/zh-cn/previous-versions/sql/sql-server-2005/ms130214(v=sql.90) 联 ...
- Oracle监控的关键指标
1.监控事例的等待 select event, , , )) "Prev", , , )) "Curr", count(*) "Tot" f ...
- nodejs Async详解之三:集合操作
Async提供了很多针对集合的函数,可以简化我们对集合进行异步操作时的步骤.如下: forEach:对集合中每个元素进行异步操作 map:对集合中的每个元素通过异步操作得到另一个值,得到新的集合 fi ...
- EasyUI Progressbar 进度条
通过 $.fn.progressbar.defaults 重写默认的 defaults. 进度条(progressbar)提供了一种显示长时间操作进度的反馈.进度可被更新以便让用户知道当前正在执行的操 ...