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]

分析:

首先笨办法,假设每个点都是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的更多相关文章

  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 Minimum Height Trees

    原题链接在这里:https://leetcode.com/problems/minimum-height-trees/ 题目: For a undirected graph with tree cha ...

  3. 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

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

  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. [Swift]LeetCode310. 最小高度树 | Minimum Height Trees

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

  7. 310. Minimum Height Trees -- 找出无向图中以哪些节点为根,树的深度最小

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

  8. Minimum Height Trees -- LeetCode

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

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

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

随机推荐

  1. MVC缓存OutPutCache学习笔记 (一) 参数配置

    OutPutCache 参数详解 Duration : 缓存时间,以秒为单位,这个除非你的Location=None,可以不添加此属性,其余时候都是必须的. Location : 缓存放置的位置; 该 ...

  2. ActionScript学习笔记

    ActionScript学习笔记 ActionScript中预定义的数据类型:Boolean.int.Number.String.uint 其中,int.Number.uint是处理数字的.int用来 ...

  3. 关于http请求

    response的Head值:  200 : 请求已成功,请求所希望的响应头或数据体将随此响应返回  304 : Not Modified 客户端有缓冲的文档并发出了一个条件性的请求,原来缓冲的文档还 ...

  4. windows 杂项

    农企amd的Radeon: 蓝宝石-镭 显卡m330只是 m200的马甲版本, nvidia的Geforce: ge: geometry : 几何很强, 精视 radeon倾向于单精度浮点计算很强, ...

  5. ASP.NET 大文件下载的实现思路及代码

    文件下载是一个网站最基本的功能,ASP.NET网站的文件下载功能实现也很简单,但是如果遇到大文件的下载而不做特殊处理的话,那将会出现不可预料的后果.本文就基于ASP.NET提供大文件下载的实现思路及代 ...

  6. 关于360的META设置,强制使用极速模式

    我的网站,为了使360浏览器打开时默认为极速模式,给用户良好的体验!避免网页由于细节而导致页面布局错乱~ <!DOCTYPE HTML> <html> <head> ...

  7. GridView控件隐藏列

    GridView隐藏列visible="false" 后你就无法取得这列的值了 下面是迄今为止最简洁的解决方法了. protected void GVList_RowDataBou ...

  8. HDOJ 4750 Count The Pairs

    按边长从小到大排序...再逐个加入(就像MST一样)最先联通的点之间最长路径中的最小值就是新加入的边的长.... Count The Pairs Time Limit: 20000/10000 MS ...

  9. 【PHP面向对象(OOP)编程入门教程】1.什么是面向对象?

    面向对象编程(Object Oriented Programming, OOP, 面向对象程序设计)是一种计算机编程架构,OOP的一条基本原则是计算机程序是由单个能够起到子程序作用的单元或对象组合而成 ...

  10. Android 全屏显示

    Android全屏显示: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInst ...