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]

Hint:

Show Hint

  1. How many MHTs can a graph have at most?

Note:

(1) According to the definition of tree on Wikipedia: “a tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree.”

(2) The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf.

public class Solution {

    /** return the height of tree if let s to be the root of the tree */
public static int bfs(ArrayList<ArrayList<Integer> > g, int s) { HashSet<Integer> upper = new HashSet<Integer> ();
HashSet<Integer> lower = new HashSet<Integer> ();
HashSet<Integer> vis = new HashSet<Integer> (); upper.add(s);
vis.add(s);
int lv = 1; while(!upper.isEmpty()) { for(int u: upper) {
ArrayList<Integer> adj = g.get(u);
for(int i=0; i<adj.size(); ++i) {
int adj_node = adj.get(i);
if(!vis.contains(adj_node)) {
lower.add(adj_node);
}
}
} if(!lower.isEmpty()) {
++lv;
} upper.clear();
for(int c: lower) {
vis.add(c);
upper.add(c);
}
lower.clear();
} return lv;
} public static ArrayList<Integer> topologicalSort(int n, int[][] edges, ArrayList<ArrayList<Integer> > g) { ArrayList<Integer> topo = new ArrayList<Integer> ();
int[] d = new int[n]; for(int i=0; i<edges.length; ++i) {
int u = edges[i][0], v = edges[i][1];
++d[u]; ++d[v];
} LinkedList<Integer> queue = new LinkedList<Integer> ();
for(int i=0; i<n; ++i) {
if(d[i] == 1) {
queue.addLast(i);
}
} while(!queue.isEmpty()) {
int top = queue.pollFirst();
topo.add(top); ArrayList<Integer> adj = g.get(top);
for(int next: adj) {
d[next]--;
if(d[next] == 1) {
queue.addLast(next);
}
}
} return topo;
} public List<Integer> findMinHeightTrees(int n, int[][] edges) { List<Integer> rs = new ArrayList<Integer> ();
if(n == 1) {
rs.add(0);
return rs;
} ArrayList<ArrayList<Integer> > g = new ArrayList<ArrayList<Integer> > ();
for(int i=0; i<n; ++i) {
ArrayList<Integer> row = new ArrayList<Integer> ();
g.add(row);
}
for(int i=0; i<edges.length; ++i) {
int u = edges[i][0];
int v = edges[i][1]; g.get(u).add(v);
g.get(v).add(u);
} HashMap<Integer, Integer> mapping = new HashMap<Integer, Integer> ();
ArrayList<Integer> topo = topologicalSort(n, edges, g); int idx = topo.get(topo.size()-1);
int min_lv = bfs(g, idx);
rs.add(idx); if(topo.size() >= 2) {
int indice = topo.get(topo.size()-2);
if(bfs(g, indice) == min_lv) {
rs.add(indice);
}
} return rs; }
}

leetcode@ [310] Minimum Height Trees的更多相关文章

  1. [LeetCode] 310. 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 解题报告(Python)

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

  4. 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_Medium tag: BFS

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

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

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

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

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

  8. LeetCode Minimum Height Trees

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

  9. Minimum Height Trees -- LeetCode

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

随机推荐

  1. Lotus Notes中编程发送邮件(二)

    在编程发送各种类似通知的邮件时,时常会需要发件人显示为某个特定的帐户,比如某个部门的名称或者管理员的名字.另一种需求是,用户收到某封邮件后,回复邮件的地址不同于发件人栏显示的地址.而正常情况下,发送邮 ...

  2. Java中ArrayList和LinkedList区别

    ArrayList和LinkedList的大致区别如下:1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构. 2.对于随机访问get和set,ArrayLis ...

  3. Windows下搭建Mysql集群

    Mysql集群的基本架构如下: 基本原理参考:[转]MySQL Cluster (集群)基本原理 这里采用最小配置,用两台机器来分别部署一个management 节点,2个data node, 2个s ...

  4. 面试题_66_to_75_Java IO 和 NIO 的面试题

    IO 是 Java 面试中一个非常重要的点.你应该很好掌握 Java IO,NIO,NIO2 以及与操作系统,磁盘 IO 相关的基础知识.下面是 Java IO 中经常问的问题. 66)在我 Java ...

  5. 数据库编程与C#编程互译

    今天有一段代码,先是用程序实现. 闲来无聊,又用存储过程实现了一次. 程序中实现. /// <summary> /// 根据区域和用户名获取可访问的国家 /// </summary& ...

  6. uva 10054 The Necklac(欧拉回路)

    明显的欧拉回路,把颜色作为点,建图后,做一遍欧拉回路.不过我是现学的,打印路径上纠结了一下,发现随着FindEuler()的递归调用的结束,不断把点压入栈中,从后向前打印,遇到"支路&quo ...

  7. apache开源项目 -- VXQuery

    Apache VXQuery 是一个兼容标准的 XML 查询处理器的实现.主要适合非常大量的 XML 数据处理. 参考: http://www.apache.org/

  8. 【C#学习笔记】读文件

    using System; using System.IO; namespace ConsoleApplication { class Program { static void Main(strin ...

  9. curl命令访问域名

    1.前言 curl是利用URL语法在命令行方式下工作的开源文件传输工具(来自百度百科).cURL 是一种简单有效的工具,可以使用cURL工具进行WEB相关的调试开发工具,相对于Yeelink这样的云平 ...

  10. 【转】Android Studio -修改LogCat的颜色*美爆了*

    原文网址:http://www.2cto.com/kf/201505/400357.html 一. 先看效果 二.设置 File->Settings 或Ctrl + Alt +S 找到 Edit ...