作者: 负雪明烛
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 :

  1. Input: n = 4, edges = [[1, 0], [1, 2], [1, 3]]
  2. 0
  3. |
  4. 1
  5. / \
  6. 2 3
  7. Output: [1]

Example 2 :

  1. Input: n = 6, edges = [[0, 3], [1, 3], [2, 3], [4, 3], [5, 4]]
  2. 0 1 2
  3. \ | /
  4. 3
  5. |
  6. 4
  7. |
  8. 5
  9. Output: [3, 4]

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.

题目大意

找出以哪些节点为根节点的时候,构建出来的整棵树的高度是最低的。

解题方法

BFS

这个题很优秀啊,是个好题。这个题给定的是个图,但是让我们构建成树,也就是说构建出来的并不是二叉树。题目其实想考我们的是,整个图最靠近中间的节点是什么。我们使用类似与拓扑排序的BFS进行解决。

拓扑排序我们都知道,每次选择入度为0的节点进行删除。在这个题中,因为我们要找到无向图最靠近中间的节点,所以,我们先使用一个字典保存每个节点的所有相邻节点set。每次把所有只有一个邻接的节点(叶子节点,类似于入度为0,但是这是个无向图,入度等于出度)都放入队列,然后遍历队列中的节点u,把和每个节点u相邻的节点v的set删去u,所以这一步操作得到的是去除了叶子节点的新一轮的图。所以我们需要再次进行选择只有一个邻接节点的叶子节点,然后放入队列中,再次操作。最后结束的标准是,整个图只留下了1个或者两个元素。为什么不能是3个呢?因为题目第一句话说了给出的图是具有树的特性的,所以一定没有环存在。

这个题整体的思路就是把所有的叶子节点放入队列中,然后同时向中间遍历,这样最后剩下来的就是整棵树中间的元素。

时间复杂度是O(V),空间复杂度是O(E + V).

  1. class Solution(object):
  2. def findMinHeightTrees(self, n, edges):
  3. """
  4. :type n: int
  5. :type edges: List[List[int]]
  6. :rtype: List[int]
  7. """
  8. if n == 1: return [0]
  9. leaves = collections.defaultdict(set)
  10. for u, v in edges:
  11. leaves[u].add(v)
  12. leaves[v].add(u)
  13. que = collections.deque()
  14. for u, vs in leaves.items():
  15. if len(vs) == 1:
  16. que.append(u)
  17. while n > 2:
  18. _len = len(que)
  19. n -= _len
  20. for _ in range(_len):
  21. u = que.popleft()
  22. for v in leaves[u]:
  23. leaves[v].remove(u)
  24. if len(leaves[v]) == 1:
  25. que.append(v)
  26. 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)的更多相关文章

  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

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

  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】1051. Height Checker 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序比较 日期 题目地址:https://leetc ...

  8. 【LeetCode】872. Leaf-Similar Trees 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 中序遍历 先序遍历 后序遍历 日期 题目地址:htt ...

  9. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

随机推荐

  1. 9 — springboot整合jdbc、druid、druid实现日志监控 — 更新完毕

    1.整合jdbc.druid 1).导入依赖 <dependency> <groupId>org.springframework.boot</groupId> &l ...

  2. 线性表A,B顺序存储合并

    线性表A,B顺序存储合并 有两张非递增有序的线性表A,B,采用顺序存储结构,两张表合并用c表存,要求C为非递减有序的,然后删除C表中值相同的多余元素.元素类型为整型 输入格式: 第一行输入输入表A的各 ...

  3. navicate连接Mysql5.7时,显示Access denied for user 'root'@'localhost' (using password: YES) 错误

    最近新装了Mysql5.7,按如下设置好了允许远程连接    (1)找到mysql配置文件并修改 sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf 将bind-ad ...

  4. Mysql的表级锁

    我们首先需要知道的一个大前提是:mysql的锁是由具体的存储引擎实现的.所以像Mysql的默认引擎MyISAM和第三方插件引擎 InnoDB的锁实现机制是有区别的.可根据不同的场景选用不同的锁定机制. ...

  5. Java Maven项目搭建

    创建空项目 New Project --> Empty Project --> ... 配置JDK Project Settings --> Project 选择JDK Module ...

  6. Linux:expr、let、for、while、until、shift、if、case、break、continue、函数、select

    1.expr计算整数变量值 格式 :expr arg 例子:计算(2+3)×4的值 1.分步计算,即先计算2+3,再对其和乘4 s=`expr 2 + 3` expr $s \* 4 2.一步完成计算 ...

  7. @PropertySource配置的用法

    功能 加载指定的属性文件(*.properties)到 Spring 的 Environment 中.可以配合 @Value 和@ConfigurationProperties 使用. @Proper ...

  8. 『与善仁』Appium基础 — 21、元素的基本操作

    目录 1.元素的基本操作说明 (1)点击操作 (2)清空操作 (3)输入操作 2.综合练习 1.元素的基本操作说明 (1)点击操作 点击操作:click()方法.(同Selenium中使用方式一致) ...

  9. 使用 scipy.fft 进行Fourier Transform:Python 信号处理

    摘要:Fourier transform 是一个强大的概念,用于各种领域,从纯数学到音频工程甚至金融. 本文分享自华为云社区<使用 scipy.fft 进行Fourier Transform:P ...

  10. DDS协议解读及测试开发实践

    DDS概述 DDS是OMG在2004年发布的中间件协议和应用程序接口(API)标准,它为分布式系统提供了低延迟.高可靠性.可扩展的通信架构标准.DDS目前在工业.医疗.交通.能源.国防领域都有广泛的应 ...