【LeetCode】310. Minimum Height Trees 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/minimum-height-trees/description/
题目描述
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 :
Input: n = 4, edges = [[1, 0], [1, 2], [1, 3]]
0
|
1
/ \
2 3
Output: [1]
Example 2 :
Input: n = 6, edges = [[0, 3], [1, 3], [2, 3], [4, 3], [5, 4]]
0 1 2
\ | /
3
|
4
|
5
Output: [3, 4]
Note:
- 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.”
- The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf.
题目大意
找出以哪些节点为根节点的时候,构建出来的整棵树的高度是最低的。
解题方法
BFS
这个题很优秀啊,是个好题。这个题给定的是个图,但是让我们构建成树,也就是说构建出来的并不是二叉树。题目其实想考我们的是,整个图最靠近中间的节点是什么。我们使用类似与拓扑排序的BFS进行解决。
拓扑排序我们都知道,每次选择入度为0的节点进行删除。在这个题中,因为我们要找到无向图最靠近中间的节点,所以,我们先使用一个字典保存每个节点的所有相邻节点set。每次把所有只有一个邻接的节点(叶子节点,类似于入度为0,但是这是个无向图,入度等于出度)都放入队列,然后遍历队列中的节点u,把和每个节点u相邻的节点v的set删去u,所以这一步操作得到的是去除了叶子节点的新一轮的图。所以我们需要再次进行选择只有一个邻接节点的叶子节点,然后放入队列中,再次操作。最后结束的标准是,整个图只留下了1个或者两个元素。为什么不能是3个呢?因为题目第一句话说了给出的图是具有树的特性的,所以一定没有环存在。
这个题整体的思路就是把所有的叶子节点放入队列中,然后同时向中间遍历,这样最后剩下来的就是整棵树中间的元素。
时间复杂度是O(V),空间复杂度是O(E + V).
class Solution(object):
def findMinHeightTrees(self, n, edges):
"""
:type n: int
:type edges: List[List[int]]
:rtype: List[int]
"""
if n == 1: return [0]
leaves = collections.defaultdict(set)
for u, v in edges:
leaves[u].add(v)
leaves[v].add(u)
que = collections.deque()
for u, vs in leaves.items():
if len(vs) == 1:
que.append(u)
while n > 2:
_len = len(que)
n -= _len
for _ in range(_len):
u = que.popleft()
for v in leaves[u]:
leaves[v].remove(u)
if len(leaves[v]) == 1:
que.append(v)
return list(que)
相似题目
207. Course Schedule
210. Course Schedule II
参考资料
http://www.cnblogs.com/grandyang/p/5000291.html
日期
2018 年 10 月 30 日 —— 啊,十月过完了
【LeetCode】310. Minimum Height Trees 解题报告(Python)的更多相关文章
- [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 ...
- 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_Medium tag: BFS
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】1051. Height Checker 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序比较 日期 题目地址:https://leetc ...
- 【LeetCode】872. Leaf-Similar Trees 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 中序遍历 先序遍历 后序遍历 日期 题目地址:htt ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
随机推荐
- R语言与医学统计图形-【20】ggplot2图例
ggplot2绘图系统--图例:guide函数.标度函数.overrides.aes参数 图例调整函数guide_legend也属于标度函数,但不能单独作为对象使用,即不能如p+guide_legen ...
- python爬虫采集
python爬虫采集 最近有个项目需要采集一些网站网页,以前都是用php来做,但现在十分流行用python做采集,研究了一些做一下记录. 采集数据的根本是要获取一个网页的内容,再根据内容筛选出需要的数 ...
- .NET Core如何配置TLS Cipher(套件)?
前言 前不久我发表了一篇关于TLS协议配置被我钻了空子,经过第三方合作伙伴验证,针对此TLS协议存在不安全套件,急催速速解决,那么我们本篇开始继续整活!第三方合作伙伴对平台安全严苛要求,我们已连续发版 ...
- MySQL全面瓦解29:使用Partition功能实现水平分区
1 回顾 上一节我们详细讲解了如何对数据库进行分区操作,包括了 垂直拆分(Scale Up 纵向扩展)和 水平拆分(Scale Out 横向扩展) ,同时简要整理了水平分区的几种策略,现在来回顾一下. ...
- A Child's History of England.19
The King was at first as blind and stubborn as kings usually have been whensoever [每当] they have bee ...
- Vue相关,diff算法。
1. 当数据发生变化时,vue是怎么更新节点的? 要知道渲染真实DOM的开销是很大的,比如有时候我们修改了某个数据,如果直接渲染到真实dom上会引起整个dom树的重绘和重排,有没有可能我们只更新我们修 ...
- 【vector的输出问题】 洛谷 P1996 约瑟夫问题
题目:P1996 约瑟夫问题 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 可恶啊,本来是一道不算难的题,硬是因为cin,cout同步流卡了我一天qwq 关闭cin,cout同步流 ...
- oracle first_value,last_valus
first_value和last_value 是用来去分析函数窗口中对应列的第一个值和最后一个值的函数. 语法如下: first_value(col [ignore NULLS]) over([PAR ...
- Can we use function on left side of an expression in C and C++?
In C, it might not be possible to have function names on left side of an expression, but it's possib ...
- zabbix之二进制安装
#:参考官方网站 https://www.zabbix.com/documentation/4.0/manual/installation/install_from_packages/debian_u ...