原题链接在这里: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 = 4edges = [[1, 0], [1, 2], [1, 3]]

        0
|
1
/ \
2 3

return [1]

Example 2:

Given n = 6edges = [[0, 3], [1, 3], [2, 3], [4, 3], [5, 4]]

     0  1  2
\ | /
3
|
4
|
5

return [3, 4]

题解:

Course ScheduleCourse 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的更多相关文章

  1. [LeetCode] Minimum Height Trees 最小高度树

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  2. [LeetCode] 310. Minimum Height Trees 解题思路

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  3. [LeetCode] 310. Minimum Height Trees 最小高度树

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  4. 【LeetCode】310. Minimum Height Trees 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 相似题目 参考资料 日期 题目地址:http ...

  5. leetcode@ [310] Minimum Height Trees

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  6. Minimum Height Trees -- LeetCode

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  7. [Swift]LeetCode310. 最小高度树 | Minimum Height Trees

    For an undirected graph with tree characteristics, we can choose any node as the root. The result gr ...

  8. Minimum Height Trees

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  9. 310. Minimum Height Trees

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

随机推荐

  1. python程序设计语言笔记 第一部分 程序设计基础

    1.1.1中央处理器(CPU) cpu是计算机的大脑,它从内存中获取指令然后执行这些指令,CPU通常由控制单元和逻辑单元组成. 控制单元用来控制和协调除cpu之外的其他组件的动作. 算数单元用来完成数 ...

  2. BZOJ2061 : Country

    记忆化搜索,设$f[i][j]$表示符号$i$一开始kmp指针为$j$,中间匹配了多少次,$g[i][j]$则表示匹配结束后kmp指针的位置. 时间复杂度$O(nl^2)$. #include< ...

  3. BZOJ2158 : Crash 的旅行计划

    A类数据: $n,q\leq1000$ 修改:$O(1)$直接改 查询:$O(n)$BFS B类数据: $n,q\leq100000$,保证是一条链 用线段树维护区间最大前缀.后缀和 修改:$O(\l ...

  4. [R语言]foreach和doParallel包实现多个数据库同时查询

    R语言在进行数据库查询时,每执行一条语句,都会阻塞.直到查询语句返回结果之后,才会进行下一条语句. 为了能够实现同时对多个数据库进行查询,以节省顺序执行下来的时间,首先考虑通过多线程来进行数据库查询. ...

  5. Linux之网络配置(不断更新中)

    ========================================================================================== 配置文件 ==== ...

  6. AppStore上传条例

    1. 条款和条件1.1 为App Store开发程序,开发者必须遵守 Program License Agreement (PLA).人机交互指南(HIG)以及开发者和苹果签订的任何协议和合同.以下规 ...

  7. fiddler 拦截小结

    一,拦截请求或响应常用命令 1.拦截命令 bpu 清除拦截请求 bpu http://www.baidu.com 拦截访问百度网站的请求.可以在 request 框的 WebForms 中改请求内容. ...

  8. ArcGIS 设置地图显示范围大小(全屏显示)

    Arcmap的FullExtent默认是地图加载的时候的extent.其实这个fullExtent是可以设置的. 打开ArcMap,选择左边图例的Layers ,右键点击,选择“Properties. ...

  9. 二 、打开地图《苹果iOS实例编程入门教程》

    该app为应用的功能为给你的iPhone打开google地图有效地址连接 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Si ...

  10. VS2010 C++环境下DLL和LIB文件目录及名称修改

    VS2010 C++环境下DLL和LIB文件目录及名称修改 转自:http://blog.csdn.net/archielau/article/details/8507581 DLL工程,Debug版 ...