310. 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]
借鉴的别人的代码:
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的更多相关文章
- 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 ...
- 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 相似题目 参考资料 日期 题目地址:http ...
- [LeetCode] Minimum Height Trees 最小高度树
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- 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 ...
- [Swift]LeetCode310. 最小高度树 | Minimum Height Trees
For an undirected graph with tree characteristics, we can choose any node as the root. The result gr ...
随机推荐
- POJ 2255 Tree Recovery 树的遍历,分治 难度:0
http://poj.org/problem?id=2255 #include<cstdio> #include <cstring> using namespace std; ...
- HDU 1166 单点更新,区间求和
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- ubuntu 14.04 安装截图工具 Shutter及使用
一.安装截图工具 Shutter 1. 添加安装包软件源 sudo add-apt-repository ppa:shutter/ppa 1 2. 更新源并安装 shutter sudo apt-ge ...
- NOIP 2013 提高组 day2 积木大赛
积木大赛 描述 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为 n 的大厦,大厦可以看成由 n 块宽度为1的积木组成,第
- IT公司100题-9-判断整数序列是不是二元查找树的后序遍历结果
问题描述: 输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果. 如果是返回true,否则返回false. 例如输入4, 8, 6, 12, 16, 14, 10,由于这一整数序列是如下树 ...
- 多线程同步内功心法——PV操作上(未完待续。。。)
阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <秒杀多线程第五篇经典线程同步关键段CS> <秒杀多线程第六篇经典线程同步事件Event& ...
- 支持.NET和移动设备的XLS读写控件XLSReadWriteII下载地址及介绍
原文来自龙博方案网http://www.fanganwang.com/product/3085转载请注明出处 读写任何单元值 数字型.字符型.布尔型以及错误型.但是你了解日期和时间型单元吗?在Exce ...
- iOS中属性Property的常用关键字的使用说明
属性关键字的作用 现在我们iOS开发中,基本都是使用ARC(自动引用计数)技术,来编写我们的代码.因此在属性property中我们经常使用的关键字有strong,weak,assign,copy,no ...
- python解无忧公主数学题108
""" python解无忧公主数学题108回文.py 题目来源: http://mp.weixin.qq.com/s?__biz=MzI5ODEwMDQyNw==& ...
- form表单select联动
下拉列表:二级联动菜单 Select对象的常用属性 options[]:返回所有option组成的一个数组: name:名称 value:option的value的值 length:设置或读取opti ...