Minimum Height Trees -- LeetCode
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]]
| / \
return [1]
Example 2:
Given n = 6
, edges = [[0, 3], [1, 3], [2, 3], [4, 3], [5, 4]]
\ | / | |
return [3, 4]
思路:找到这棵树中所有度为1的节点,它们是最外层的节点。从树中移除这些节点,重复这个操作直到树中的节点数小于3。结果可能是1个节点或者两个节点,这取决于树中最长路径的长度(奇偶性)。时间复杂度O(N)。
class Solution {
public:
vector<int> findMinHeightTrees(int n, vector<pair<int, int>>& edges) {
if (n == ) return vector<int>(, );
vector<unordered_set<int> > tree(n, unordered_set<int>());
for (int i = ; i < edges.size(); i++) {
tree[edges[i].first].insert(edges[i].second);
tree[edges[i].second].insert(edges[i].first);
}
vector<int> leaves;
for (int i = ; i < n; i++)
if (tree[i].size() == ) leaves.push_back(i);
while (n > ) {
vector<int> newLeaves;
for (int i = ; i < leaves.size(); i++)
for (auto j : tree[leaves[i]]) {
tree[j].erase(leaves[i]);
if (tree[j].size() == ) newLeaves.push_back(j);
}
n -= leaves.size();
leaves = newLeaves;
}
return leaves;
}
};
Minimum Height Trees -- LeetCode的更多相关文章
- [LeetCode] Minimum Height Trees 最小高度树
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- LeetCode Minimum Height Trees
原题链接在这里:https://leetcode.com/problems/minimum-height-trees/ 题目: For a undirected graph with tree cha ...
- [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 ...
- 【LeetCode】310. Minimum Height Trees 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 相似题目 参考资料 日期 题目地址:http ...
- leetcode@ [310] 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 ...
- Minimum Height Trees
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- 310. Minimum Height Trees
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
随机推荐
- 【STSRM10】数学上来先打表
[算法]DP+数学计数 [题意]给出n个点(不同点之间有区别),求出满足下列条件的连边(双向边)方案(对1004535809取模): 1.每条边连接两个不同的点,每两个点之间至多有一条边. 2.不存在 ...
- 【BZOJ】1202: [HNOI2005]狡猾的商人
[题意]w组数据,给定n和m,给出m段区间[s,t](1<=s<=t<=n)的数字和,求是否矛盾.n<100,m<1000,w<100. [算法]带权并查集 [题解 ...
- js_layer弹窗的使用和总结
2018-04-10 一张呈现给用户的网页,会有很多种交互,比如连不上网络,用户点击按钮时向后台请求数据不成功等等.像这些情况,用户是看不见的, 要给用户更好的体验,在特定的时间,给客户反馈内容.实时 ...
- Oracle with重用子查询
--with 重用子查询对于多次使用相同子查询的复杂查询语句来说,用户可能会将查询语句分成两条语句执行.第一条语句将子查询结果存放到临时表,第二条查询语句使用临时表处理数据.从 Oracle 9i 开 ...
- caffe Python API 之InnerProduct
net.fc3 = caffe.layers.InnerProduct(net.pool1, num_output=1024, weight_filler=dict(type='xavier'), b ...
- Python数据处理实战
一.运行环境 1.python版本 2.7.13 博客代码均是这个版本2.系统环境:win7 64位系统 二.需求 对杂乱文本数据进行处理 部分数据截图如下,第一个字段是原字段,后面3个是清洗出的字段 ...
- PHP配置Configure报错:Please reinstall the libzip distribution
PHP配置Configure报错:Please reinstall the libzip distribution 发生情景: php执行配置命令configure时,报如下错误: checking ...
- sleep() 函数
函数名: sleep 功 能: 执行挂起一段时间 用 法: unsigned sleep(unsigned seconds); 头文件 #include <windows.h> # wi ...
- [转]windows消息机制(MFC)
消息分类与消息队列 Windows中,消息使用统一的结构体(MSG)来存放信息,其中message表明消息的具体的类型, 而wParam,lParam是其最灵活的两个变量,为不同的消息类型时,存放数据 ...
- 通过IP地址和子网掩码计算主机数
知道ip地址和子网掩码后可以算出: 1. 网络地址 2. 广播地址 3. 地址范围 4. 本网有几台主机 例1:下面例子IP地址为192·168·100·5 子网掩码是255·255·255·0.算出 ...