-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
310 Minimum Height Trees.swift
65 lines (52 loc) · 1.8 KB
/
310 Minimum Height Trees.swift
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//
// 310 Minimum Height Trees.swift
// LeetCode-Solutions
//
// Created by Aleksandar Dinic on 04/11/2020.
// Copyright © 2020 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://leetcode.com/problems/minimum-height-trees/
class Solution {
/// Finds a list of all minimum height trees root labels.
///
/// - Parameters:
/// - n: The number of nodes in the graph.
/// - edges: An array of edges.
/// - Returns: The list of all minimum height trees roots labels.
///
/// - Complexity:
/// - time: O(n), where n is the number of nodes in the graph.
/// - space: O(n), where n is the number of nodes in the graph.
func findMinHeightTrees(_ n: Int, _ edges: [[Int]]) -> [Int] {
guard n > 1 else { return [0] }
var neighbors = Array(repeating: Set<Int>(), count: n)
var inDegrees = Array(repeating: 0, count: n)
for edge in edges {
neighbors[edge[0]].insert(edge[1])
neighbors[edge[1]].insert(edge[0])
inDegrees[edge[0]] += 1
inDegrees[edge[1]] += 1
}
var leaves = [Int]()
for (node, degree) in inDegrees.enumerated() {
guard degree == 1 else { continue }
leaves.append(node)
}
var n = n
while n > 2 {
n -= leaves.count
var newLeaves = [Int]()
for leaf in leaves {
guard let neighbor = neighbors[leaf].first else { continue }
neighbors[neighbor].remove(leaf)
inDegrees[leaf] -= 1
inDegrees[neighbor] -= 1
guard inDegrees[neighbor] == 1 else { continue }
newLeaves.append(neighbor)
}
leaves = newLeaves
}
return leaves
}
}