0%

leetcode Minimum Height Trees

leetcode 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]]

1
2
3
4
5
  0

1
/ \
2 3

return [1]

Example 2:

Given n = 6, edges = [[0, 3], [1, 3], [2, 3], [4, 3], [5, 4]]

1
2
3
4
5
6
7
0  1  2
\ /
3

4

5

return [3, 4]

Hint:

  1. How many MHTs can a graph have at most?

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.

今天新出的题 leetcode Minimum Height Trees

题意:

给定一个n个结点n-1条边的无向图(就是树啦),让你找从哪个点出发,到其他结点的最长距离最小?(返回所有答案)

思路:

一开始类似RIP来更新距离,结果TLE。证明O(n^2)的复杂度太大

只好想其他的方法。

答案一定是最长距离的中间结点位置上。

我们要的是中间结点,沿着树的外围每次把叶子结点砍掉,那么,最后剩下的不就是中间结点了么?

Leetcode其他树的题:  leetcode Tree 整理版

Code

Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# leetcode Minimum Height Trees
# https://www.hrwhisper.me/leetcode-minimum-height-trees/
class Solution(object):
def findMinHeightTrees(self, n, edges):
"""
:type n: int
:type edges: List[List[int]]
:rtype: List[int]
"""
if n==1: return [0]

digree = [0 for i in xrange(n)]
g = [[] for i in xrange(n)]
for x,y in edges:
digree[x] += 1
digree[y] += 1
g[x].append(y) #add_edge
g[y].append(x)

leaves = [i for i in xrange(n) if digree[i]==1]
nodes = n
while nodes > 2:
temp = []
for i in leaves:
digree[i] = 0
nodes -= 1
for j in g[i]:
digree[j] -= 1
if digree[j] == 1:
temp.append(j)
leaves = temp
return leaves

 

附上一开始TLE的版本

思路就是类似于RIP算法,通过相邻的结点更新当前结点距离。

比如说有x ,y 一条边(以x=>y举例,y=>x同样进行更新)

我们可以通过y到各个结点的距离dis[y]来更新,就是说,让x走y这条边,然后到其他结点的距离(比如i)是不是比当前不走y到i的距离小。

于是有:dis[x][i] = dis[i][x] = min(dis[i][x], dis[i][y] + 1)  (无向图对称性:dis[i][y] = dis[y][i] , dis[x][i] = dis[i][x])

最后找最小的即可。

总复杂度:O(n^2)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution(object):
def findMinHeightTrees(self, n, edges):
"""
:type n: int
:type edges: List[List[int]]
:rtype: List[int]
"""
INF = 0x7ffffffe
dis = [[INF for j in xrange(n)] for i in xrange(n)]
for i in xrange(n): dis[i][i] = 0
g = [[0 for j in xrange(n)] for i in xrange(n)]
for x,y in edges:
g[x][y] = g[y][x] = 1

for x,y in edges:
dis[x][y] = dis[y][x] = 1
for i in xrange(n):
dis[y][i] = dis[i][y] = min(dis[i][y], dis[i][x] + 1)
dis[x][i] = dis[i][x] = min(dis[i][x], dis[i][y] + 1)

nodes_height = [max(d) for d in dis]
min_dis = min(nodes_height)
return [i for i,c in enumerate(nodes_height) if c == min_dis]
请我喝杯咖啡吧~