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. DataGridview 绑定泛型List<T>

    .DataGridView数据绑定对比(DataTable与泛型List): 当DataGridView的DataSource是DataTable的时候,DataTable的数据改变时,DataGri ...

  2. 浅谈JavaScript中的事件

    引言 Html页面与JavaScript之间的交互是通过事件来完成的.事件,就是文档或者浏览器窗口中发生的一些特定的交互瞬间.可以使用侦听器(处理程序)来预订事件,以便事件发生时执行相应的代码.这在传 ...

  3. Hadoop快速入门

    目的 这篇文档的目的是帮助你快速完成单机上的Hadoop安装与使用以便你对Hadoop分布式文件系统(HDFS)和Map-Reduce框架有所体会,比如在HDFS上运行示例程序或简单作业等. 先决条件 ...

  4. 【struts2】Result和ResultType

    简单的说,Result是Action执行完后返回的一个字符串,它指示了Action执行完成后,下一个页面在哪里.Result仅仅是个字符串,仅仅是用来指示下一个页面的,那么如何才能够到达下一个页面呢? ...

  5. Java中为什么有abstract interface 修饰类?

    如果有人问你为什么有abstract interface 修饰类,答案一定是他看到的这种方式一定是反编译出来的结果.实际中abstract interface和interface修饰的类没有区别. 下 ...

  6. Memcached 简介、安装和基本使用

    本章包括内容: 一.Memcached是什么.能干什么.特点; 二.在Centos7上安装Memcached 三.Memcached启动.简单操作.关闭------------------------ ...

  7. sql拼音简写函数

    USE [HotelDB]GO /****** Object: UserDefinedFunction [dbo].[fn_GetPy] Script Date: 2016/1/4 13:29:13 ...

  8. linux下gedit读取txt乱码解决办法

    修改一下gedit的设置来让它显示的txt不再是乱码: 你可以通过以下步骤,使 gedit 正确显示中文编码文件. 按下 Alt-F2,打开“运行应用程序”对话框.在文本框中键入“gconf-edit ...

  9. 在PHP中$_SESSION的使用方法

    使用PHP应用session时,将session中的数据存储在服务器上,然后通过客户端传来的sessionID识别客户端的信息,并提取信息. php中的session的常用操作:session的写入. ...

  10. 第26天 swift reflect

    1.reflect http://www.jianshu.com/p/b5c87824e33c 2.android studio 2.0项目1.5编译 Cause: com/android/build ...