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 ...
随机推荐
- JStorm之Nimbus简介
本文导读: ——JStorm之Nimbus简介 .简介 .系统框架与原理 .实现逻辑和代码剖析 )Nimbus启动 )Topology提交 )任务调度 )任务监控 .结束语 .参考文献 附:JStor ...
- Hadoop2.2.0 hive0.12 hbase0.94 配置问题记录
环境:centos6.2 Hadoop2.2.0 hive0.12 hbase0.94 1>hadoop配好之后,跑任务老失败,yarn失败,报out of memory错误,然后怎么调整内存大 ...
- HDU 2836 (离散化DP+区间优化)
Reference:http://www.cnblogs.com/wuyiqi/archive/2012/03/28/2420916.html 题目链接: http://acm.hdu.edu.cn/ ...
- JS 用sort方法排序字符串
JavaScript提供了一种更简便的方法用于比较两个字符串——localeCompare(),localeCompare()使用本地特定的顺序来比较两个字符串,语法如下:string.localeC ...
- iOS 网络框架编写总结
一,常用 1> 不错的处理接收到的网络图片数据的方法 id img= ISNSNULL(pic)?nil:[pic valueForKey:@"img"]; NSString ...
- Linux下bash: scp: command not found问题 或者装ssh包时报错 Requires: libedit.so.0()(64bit)
一.用scp命令从物理主机向CentOS 6.1虚拟机传送文件,提示以下错误:bash: scp: command not found到CentOS 6.1虚拟机查看也缺少scp命令.该虚拟机 ...
- ubuntu 装机及装机以后干的事情
一.装系统 下载ubuntu镜像 ubuntu 16.04 镜像下载(linux公社) 安装unetbootin (u盘启动盘制作工具) sudo apt-get install unetbootin ...
- Maven_非法字符: '\ufeff' 解决方案
Idea在maven打包时报非法字符: '\ufeff' ,但打开报错的类看没有问题,后来发现是隐蔽字符BOM的问题,解决办法是用Notepad++打开这个类,然后改变编码格式为UTF-8 无DOM ...
- 用jQuery与JSONP轻松解决跨域访问的问题
浏览器端的真正跨域访问,推荐的是目前jQuery $.ajax()支持get方式的跨域,这其实是采用jsonp的方式来完成的. var qsData = {'searchWord':$("# ...
- How does controller listen to service?
Polling. The Controller periodically asks the Service for the latest data. IMHO, this option sucks, ...