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]
分析:
首先笨办法,假设每个点都是root, 然后利用BFS,看那个root到最后一个leaf的高度是多少,如果比目前找到的更小,则更新,如果相同,则把那个点加到list里。
public List<Integer> findMinHeightTrees(int n, int[][] edges) {
List<Integer> all = new ArrayList<Integer>();
if (n <= ) {
all.add();
return all;
}
Map<Integer, List<Integer>> map = new HashMap<>();
for (int i = ; i < n; i++) {
map.put(i, new ArrayList<Integer>());
}
for (int[] edge : edges) {
map.get(edge[]).add(edge[]);
map.get(edge[]).add(edge[]);
}
int minHeight = Integer.MAX_VALUE;
List<Integer> temp = new ArrayList<>();
for (int i = ; i < n; i++) {
int height = ;
boolean[] visited = new boolean[n];
visited[i] = true;
List<Integer> list = map.get(i);
while (list.size() != ) {
for (Integer k : list) {
if (!visited[k]) {
visited[k] = true;
temp.addAll(map.get(k));
}
}
list = temp;
temp = new ArrayList<>();
height++;
}
if (height < minHeight) {
all.clear();
all.add(i);
minHeight = height;
} else if (height == minHeight) {
all.add(i);
}
}
return all;
}
更好的方法,先构成一棵树,把数的叶子逐层的砍掉(叶子的degree为1),当这棵树只剩下2颗或者不到两颗的节点的时候,就停止。
public class Solution {
public List<Integer> findMinHeightTrees(int n, int[][] edges) {
List<Integer> leaves = new ArrayList<Integer>();
if (n <= ) {
leaves.add();
return leaves;
}
List<Set<Integer>> graph = new ArrayList<>();
for (int i = ; i < n; i++) {
graph.add(new HashSet<Integer>());
}
for (int[] edge : edges) {
graph.get(edge[]).add(edge[]);
graph.get(edge[]).add(edge[]);
}
for (int i = ; i < n; i++) {
if (graph.get(i).size() == ) {
leaves.add(i);
}
}
while (n > ) {
n -= leaves.size();
List<Integer> newLeaves = new ArrayList<>();
for (int leave : leaves) {
for (int newLeaf : graph.get(leave)) {
graph.get(newLeaf).remove(leave);
if (graph.get(newLeaf).size() == ) {
newLeaves.add(newLeaf);
}
}
}
leaves = newLeaves;
}
return leaves;
}
}
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 Minimum Height Trees
原题链接在这里:https://leetcode.com/problems/minimum-height-trees/ 题目: For a undirected graph with tree cha ...
- 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 解题思路
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 ...
- 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 ...
- [LeetCode] 310. Minimum Height Trees 最小高度树
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
随机推荐
- webapi输入验证过滤器ValidationActionFilter
public class validationActionFilter:ActionFilterAttribute { public override void OnActionExecuting(S ...
- 使用 Elmah一些要注意的问题
http://www.cnblogs.com/apsnet/archive/2012/04/28/2474730.html 1. Elmah使用后,在发布时,要区分IIS6和IIS7 ,IIS6下 H ...
- html 表格head头部不动 body部分滚动,每格宽同内容增加
如下图同Excel表格首行固定: <style> .table{ width: 100%; border-collapse:collapse; border-spacing:0;} .ta ...
- ASP.NET MVC使用Bootstrap系列(4)——使用JavaScript插件
阅读目录 序言 Data属性 VS 编程API 下拉菜单(dropdown.js) 模态框(modal.js) 标签页(tab.js) 工具提示(tooltip.js) 弹出框(popover.js) ...
- MongoDB设置访问权限、设置用户(转)
MongoDB已经使用很长一段时间了,基于MongoDB的数据存储也一直没有使用到权限访问(MongoDB默认设置为无权限访问限制),今天特地花了一点时间研究了一下,研究成果如下: 注:研究成果基于W ...
- Ajax异步刷新地址栏
公司项目后台使用现成的UI框架,DevExpress,jqGrid,XXXUI之类的,这些展示数据列表的控件/插件,基本是异步的. 这倒也好,有变化也只是数据那一块变化,不会重新加载整个页面. 但是, ...
- 【采集层】Kafka 与 Flume 如何选择--转自悟性的博文
[采集层]Kafka 与 Flume 如何选择 收藏 悟性 发表于 2年前 阅读 23167 收藏 16 点赞 4 评论 1 摘要: Kafka, Flume 采集层 主要可以使用Flume, Kaf ...
- PPTP(Point to Point Tunneling Protocol),即点对点隧道协议。
PPTP PPTP(Point to Point Tunneling Protocol),即点对点隧道协议.该协议是在PPP协议的基础上开发的一种新的增强型安全协议,支持多协议虚拟专用网(VPN),可 ...
- 【8-23】node.js学习笔记
Node入门 //请求(require)Node.js自带的 http 模块,并且把它赋值给 http 变量 //变成了一个拥有所有 http 模块所提供的公共方法的对象 var http = req ...
- Linux 运行 apt-get install 就出现jdk installer 错误的解决方法
解决办法如下: sudo rm /var/lib/dpkg/info/oracle-java7-installer* sudo apt-get purge oracle-java7-installer ...