LeetCode Minimum Height Trees
原题链接在这里:https://leetcode.com/problems/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]
题解:
与Course Schedule, Course Schedule II类似。
用BFS based topological sort. 从叶子节点开始放入queue中,最后剩下的一个或者两个就是最中心的点.
这里练习undirected graph的topological sort. 用Map<Integer, Set<Integer>>来表示graph, 一条edge, 两头node都需要加graph中.
Time Complexity: O(n+e). Space: O(n+e).
AC Java:
class Solution {
public List<Integer> findMinHeightTrees(int n, int[][] edges) {
List<Integer> res = new ArrayList<Integer>();
if(n == 1){
res.add(0);
return res;
}
if(n < 1 || edges == null || edges.length == 0){
return res;
}
HashMap<Integer, HashSet<Integer>> graph = new HashMap<Integer, HashSet<Integer>>();
for(int i = 0; i<n; i++){
graph.put(i, new HashSet<Integer>());
}
for(int [] edge : edges){
graph.get(edge[0]).add(edge[1]);
graph.get(edge[1]).add(edge[0]);
}
LinkedList<Integer> que = new LinkedList<Integer>();
for(Map.Entry<Integer, HashSet<Integer>> entry : graph.entrySet()){
if(entry.getValue().size() == 1){
que.add(entry.getKey());
}
}
while(n > 2){
n -= que.size();
LinkedList<Integer> temp = new LinkedList<Integer>();
while(!que.isEmpty()){
int cur = que.poll();
for(int neigh : graph.get(cur)){
graph.get(cur).remove(neigh);
graph.get(neigh).remove(cur);
if(graph.get(neigh).size() == 1){
temp.add(neigh);
}
}
}
que = temp;
}
res.addAll(que);
return res;
}
}
LeetCode Minimum Height Trees的更多相关文章
- [LeetCode] 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 ...
- 【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 ...
- Minimum Height Trees -- LeetCode
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 ...
随机推荐
- hiveserver2
hiveserver2 默认绑定了ip:localhost 和 port:10000 !connect jdbc:hive2://localhost:10000 org.apache.hive.jd ...
- Theano深度学习结构分析
Reference:Theano入门三部曲 http://deeplearning.net/tutorial/logreg.html (Softmax回归) http://deeplearning. ...
- RequireJS 循环依赖报 模块undefined 处理方案
RequireJS 循环依赖 开始学习使用RequireJS之后做了几个小例子,之后想着把手头的项目也用RequireJS写一遍试试.感觉胜利就在前方了,忽然发现始终卡在一个问题上: 很常见的一个问题 ...
- Codeforces Round #249 (Div. 2) A. Black Square
水题 #include <iostream> #include <vector> #include <algorithm> using namespace std; ...
- ACM 数独
数独 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 数独是一种运用纸.笔进行演算的逻辑游戏.玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一 ...
- xenu工具介绍
Xenu Link Sleuth 也许是你所见过的最小但功能最强大的检查网站死链接的软件了.你可以打开一个本地网页文件来检查它的链接,也可以输入任何网址来检查.它可以分别列出网站 的活链接以及死链接, ...
- 淘金客II项目问题日志(AngularJs+BootStrap+Api接口开发)
问题二: 组件,如果是modal框,那么show的时候,是不会load它的(因为load有时候是需要传送数据的),需要别的组件去主动load它.那么问题来了:哪些框直接show,哪些框不仅需要show ...
- 利用Jquery给当前页或者跳转后页面的导航栏添加选中后样式
具体的样式有两种情况,一种是直接给当前页面添加特殊样式,当网页刷新或者跳转到下一页后,样式消失:另一种情况是即使刷新页面后样式仍然有效. 直接上代码: 第一种情况: 在CODE上查看代码片派生到我的代 ...
- Qt 控件随窗口缩放
在Qt的界面设计中,我们有时候希望窗口在最大化的时候,上面的控件也跟着缩放,那么我们就需要调整控件的SizePolicy属性,关于这个属性的讲解请参见我之前的博客Qt SizePolicy 属性,由于 ...
- 20个有用的jq弹窗
链接地址:http://www.ffpic.com/zixun/20-useful-jquery-modal-plugins.html 推荐:jQuery.bPopup.js