-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimumHeightTrees.py
More file actions
33 lines (31 loc) · 976 Bytes
/
minimumHeightTrees.py
File metadata and controls
33 lines (31 loc) · 976 Bytes
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
33
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/minimum-height-trees/
# Author: Miao Zhang
# Date: 2021-02-01
class Solution:
def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]:
'''
leaves[u] = v: v->u
remove leaf nodes
'''
from collections import defaultdict
from collections import deque
if n == 1 and len(edges) == 0: return [0]
leaves = defaultdict(set)
for u, v in edges:
leaves[u].add(v)
leaves[v].add(u)
q = deque()
for u, v in leaves.items():
if len(v) == 1:
q.append(u)
while n > 2:
n -= len(q)
for _ in range(len(q)):
u = q.popleft()
for v in leaves[u]:
leaves[v].remove(u)
if len(leaves[v]) == 1:
q.append(v)
return list(q)