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 ...
随机推荐
- sosi-statistics
set echo offset scan onset lines 150set pages 66set verify offset feedback offset termout offcolumn ...
- Linux下批量管理工具pssh使用记录
pssh是一款开源的软件,使用python实现,用于批量ssh操作大批量机器:pssh是一个可以在多台服务器上执行命令的工具,同时支持拷贝文件,是同类工具中很出色的:比起for循环的做法,我更推荐使用 ...
- QQ 空间过滤器 for V8
最近 QQ空间升级到 V8 版本,做了很大的调整, 我也做了升级,由于时间关系,功能暂时只有 模块过滤,其他过滤请等待后续更新,谢谢大家的支持! 刚刚上线,不知道你们能否看到 https://chro ...
- Squirrel语言初探(可以使用VC6或者MinGW编译)
Squirrel语言初探 为啥我要关注Squirrel语言?原来Squirrel就很像我希望设计出的理想中的语言(当然也不完全符合).比如我觉得Lua的语法表述不清晰,累赘,于是想用C系语法来代替Lu ...
- sailsjs入门到精通(一)
sailsjs 官方网站http://sailsjs.com/ 中文网站: http://sailsdoc.swift.ren/ 1 全局安装sails npm install sails@bet ...
- appfog 添加数据库支持
1.PhpMyAdmin与app 在同一应用 1.cd进入应用所在的文件夹,输入 git clone git://github.com/appfog/af-php-myadmin.git 2.进入本地 ...
- Python扩展之类的魔术方法
Python中类的魔术方法 在Python中以两个下划线开头的方法,__init__.__str__.__doc__.__new__等,被称为"魔术方法"(Magic method ...
- Flask系列之自定义中间件
from flask import Flask app = Flask(__name__) @app.route('/index') def index(): return 'Hello World' ...
- 自定义centos7 yum仓库
将安装光盘插入 mkdir /newyum umount /dev/sr0 mount /dev/sr0 /media cp -rf /media/Packages /newyum #将镜像中的rpm ...
- http之请求方法
根据HTTP标准,HTTP请求可以使用多种请求方法.HTTP1.0定义了三种请求方法: GET, POST 和 HEAD方法.HTTP1.1新增了五种请求方法:OPTIONS, PUT, DELETE ...