原题链接在这里: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. soapui中文操作手册(五)----入门与安全测试

    在SoapUI4.0引入的安全测试特点使它非常容易为你来验证你的目标服务的功能性安全,就可以评估您的系统常见的安全攻击的漏洞.特别是如果系统是公开可用的,即使不是这种情况,确保了完全安全的环境也是非常 ...

  2. BZOJ1798[Ahoi2009]Seq 维护序列seq 题解

    题目大意: 有长为N的数列,有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2)把数列中的一段数全部加一个值; (3)询问数列中的一段数的和,由于答案可能很大,你只需输出这个数模P的值. ...

  3. topcoder SRM 624 DIV2 BuildingHeightsEasy

    从大到小遍历一遍,每次取M个元素,然后求得最小的floor即可 int minimum(int M, vector <int> heights) { sort(heights.begin( ...

  4. 【BZOJ】3712: [PA2014]Fiolki

    http://www.lydsy.com/JudgeOnline/problem.php?id=3712 题意:n个瓶子,第i个瓶子里又g[i]克物质.m次操作,第i次操作把第a[i]个瓶子的东西全部 ...

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

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

  6. ffmpeg解码

    解码流程 http://www.cnblogs.com/lidabo/p/4582391.html 例子 http://www.cnblogs.com/lidabo/p/4582393.html

  7. 20145330孙文馨 《Java程序设计》第二周学习总结

    20145330孙文馨第二周学习总结 第二周相比于第一周对java语言有了深一点的了解,也意识到多敲代码才是学习计算机语言的最好方法. 教材内容总结 类型.变量与运算符 *基本类型 整数(short. ...

  8. SolrCloud-如何在.NET程序中使用

    https://github.com/vladen/SolrNet 原来我们在我们的项目里用的是根据数据库路由到不同的单机Solr服务器,但是这样的话,每次Solr配置的修改都要修改三台不通的服务器, ...

  9. C#中Post和Get提交

    1.Post提交 private string PostWebRequest(string Url, string paramData, string dataEncode) { string ret ...

  10. flume-ng配置文档简单说明

    1.配置文件现状 1.1 Flume数据接收端 IP地址:54.0.95.67 功能:接收各个端口发来的数据. 启动方式:进入目录 /usr/local/flume/*bin 在终端运行 ./rece ...