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]

借鉴的别人的代码:

 public class Solution {
public List<Integer> findMinHeightTrees(int n, int[][] edges) { List<Integer> leaf=new ArrayList<>();
if(n<=1)
{
leaf.add(0);
return leaf;
} Map<Integer,List<Integer>> graph=new HashMap<>();
for(int i=0;i<n;i++)
graph.put(i,new ArrayList()); int[] neighbors=new int[n];
for(int[] edge:edges)
{
neighbors[edge[0]]++;
neighbors[edge[1]]++;
graph.get(edge[0]).add(edge[1]);
graph.get(edge[1]).add(edge[0]);
} for(int i=0;i<n;i++)
{
if(graph.get(i).size()==1)
leaf.add(i);
} while(n>2)
{
List<Integer> newleaf=new ArrayList<>();
for(int l:leaf)
{
n--;
for(int nb:graph.get(l))
{
if(--neighbors[nb]==1)
newleaf.add(nb);
} }
leaf=newleaf;
}
return leaf;
}
}

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. 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 解题报告(Python)

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

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

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

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

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

随机推荐

  1. bzoj 1854: [Scoi2010]游戏

    #include<cstdio> #include<iostream> #include<cstring> #define M 2000008 using name ...

  2. Android webview 取得javascript返回值

    package com.she.jyass.UI; import android.content.Context; import android.webkit.WebView; public clas ...

  3. ubuntu 14.04 安装 foxit pdf阅读器

    1.官网下载 http://www.foxitsoftware.cn/downloads/ 2.安装 tar -zxvf FoxitReader1.01.0925_Server_x64_enu_Set ...

  4. svnadmin:error while loading shared libraries: libaprutil-1.so.0:cannot open shared object file: No such file or directory

    wdcp下安装svn后一直提示 svnadmin:error while loading shared libraries: libaprutil-1.so.0:cannot open shared ...

  5. Javascript——Math对象

    Math 对象是一个固有的对象,无需创建它,直接把 Math 作为对象使用就可以调用其所有属性和方法.这是它与Date,String对象的区别  Math 对象属性 Math 对象方法  

  6. POJ 1777 mason素数

    题目大意: 给定数列 a1 , a2 , ... , an 希望找到一个  N = sigma(ai^ki)  , (0<=ki<10) ,ki可随自己定为什么 只要保证N的因子和可以表示 ...

  7. 神州通,我看行---K2用户交流会华南站

    主题:K2高级移动信息化解决方案DBToApp开发工具 嘉宾:神州通在线 张德阔 移动办公APP开发≠一般APP开发,你知道这几种企业管理移动APP开发模式吗? 原生APP开发模式Native 具有最 ...

  8. linux exec用法总结

    Linux中exec的用法总结 先总结一个表: exec命令 作用 exec ls 在shell中执行ls,ls结果显示结束后不返回原来的的目录中,而是/(根目录) exec <file 将fi ...

  9. C/C++中函数参数传递详解(一)

    *在定义时使用代表指针类型,其他情况代表取内容.&在定义时使用代表引用(别名),在其他情况代表取地址 在编写个人函数的时候,你将会受到C++中的一条基本的原则的限制:在默认的情况下,变量只能以 ...

  10. How To Create A Struts 2 Web Application

    以简单登录为例 1.创建一个Dynamic Web projec项目记得勾选Generate web.xml deployment dsecriptor 2.引入Struts 2工程所需运行库文件 解 ...