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 exactlyone 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.

这个题目思路来自于Solution, 类似于剥洋葱, 从leaves开始一层一层往里面剥, 依次更新leaves, 到最后只剩一个或2个nodes的时候(因为根据tree的定义, 从数学上可以证明最多只有两个root的height一样), 那么剩下的点就是答案.

1. Constraints

1) 题目意思已经很清楚了, 只是需要判断edge case: n = 1, n= 2 时

2. Ideas

类似于BFS的解法      T: O(n)    S: O(n)

3. Code

class Solution:
def miniHeightTree(self, n, edges):
if n <3: return [i for i in range(n)]
graph = collections.defaultdict(set)
for c1, c2 in edges:
graph[c1].add(c2)
graph[c2].add(c1)
leaves = [i for i in range(n) if len(graph[i]) == 1]
while n > 2:
n -= len(leaves)
newleaves = []
for i in leaves: #take off the outside leaves
neig = graph[i].pop() # neighbour of the leave
graph[neig].remove(i) #remove the edge from leave to neighbor
if len(graph[neig]) == 1:
newleaves.append(neig)
leaves = newleaves
return leaves

[LeetCode] 310. Minimum Height Trees_Medium tag: BFS的更多相关文章

  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. 【LeetCode】310. Minimum Height Trees 解题报告(Python)

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

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

    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] 64. Minimum Path Sum_Medium tag: Dynamic Programming

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  8. [LeetCode] 127. Word Ladder _Medium tag: BFS

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  9. [LeetCode] 301. Remove Invalid Parentheses_Hard tag:BFS

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

随机推荐

  1. vue2.0在table中实现全选和反选

    其实在去年小颖已经写过一篇:Vue.js实现checkbox的全选和反选 小颖今天在跟着慕课网学习vue的过程中,顺便试试如何在table中实现全选和反选,页面的css样式是直接参考慕课网的样式写的, ...

  2. ELK系列四:Logstash的在ELK架构中的使用和简单的输入

    1.ELK架构中Logstash的位置: 1.1.小规模集群部署(学习者适用的架构) 简单的只有Logstash.Elasticsearch.Kibana,由Logstash收集日志或者流量信息,过滤 ...

  3. 【BZOJ1417】Pku3156 Interconnect 记忆化搜索

    [BZOJ1417]Pku3156 Interconnect Description 给出无向图G(V, E). 每次操作任意加一条非自环的边(u, v), 每条边的选择是等概率的. 问使得G连通的期 ...

  4. 复杂度O(n)计算

    https://blog.csdn.net/daijin888888/article/details/66970902 一.算法的时间复杂度定义 在进行算法分析时,语句总的执行次数T(n)是关于问题规 ...

  5. Shell输入输出重定向

    全部可用的重定向命令列表 命令 说明 command > file 将输出重定向到 file. command < file 将输入重定向到 file. command >> ...

  6. 美团店铺评价语言处理以及文本分类(logistic regression)

    美团店铺评价语言处理以及分类(LogisticRegression) 第一篇 数据清洗与分析部分 第二篇 可视化部分, 第三篇 朴素贝叶斯文本分类 本文是该系列的第四篇 主要讨论逻辑回归分类算法的参数 ...

  7. django的queryset和objects对象

    1. queryset是查询集,就是传到服务器上的url里面的内容.Django会对查询返回的结果集QerySet进行缓存,这里是为了提高查询效率. 也就是说,在你创建一个QuerySet对象的时候, ...

  8. Print or Cout an Unsigned Char Variable 打印无符号字符

    在C++中,unsigned char用来表示一个字节,也就是8位大小的值,那么我们如何来打印出其值呢,用cout直接打印会乱码,我们可以通过下面两种方法来打印: cout << stat ...

  9. MatLab Mark Points 给点标序号

    在MatLab中,我们有时要给画出的点按一定顺序标上序号,下面这个简单的例子实现这个功能: x=rand(,); y=rand(,); plot(x,y,'r*'); text(x+:));

  10. [LintCode] Find the Missing Number 寻找丢失的数字

    Given an array contains N numbers of 0 .. N, find which number doesn't exist in the array. Example G ...