-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
61 lines (60 loc) · 1.71 KB
/
Copy pathsolution.java
File metadata and controls
61 lines (60 loc) · 1.71 KB
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
// 310. Minimum Height Trees
// https://leetcode.com/problems/minimum-height-trees/
// Medium | Java | Accepted 2026-08-27
// Runtime 15 ms | Memory 63.7 MB
class Solution {
public List<Integer> findMinHeightTrees(int n, int[][] edges) {
if(n==1)
{
return new ArrayList<>(Arrays.asList(0));
}
int[] frequency = new int[n];
List<Integer>[] graph = new ArrayList[n];
for(int[] e : edges)
{
if(graph[e[0]]==null)
{
graph[e[0]] = new ArrayList<>();
}
if(graph[e[1]]==null)
{
graph[e[1]] = new ArrayList<>();
}
graph[e[0]].add(e[1]);
graph[e[1]].add(e[0]);
frequency[e[1]]++;
frequency[e[0]]++;
}
Queue<Integer> bfs = new LinkedList<>();
for(int i = 0; i<frequency.length; i++)
{
if(frequency[i]==1)
{
bfs.add(i);
}
}
// [5, 0, 1, 2]
int numberNodes = n;
while(numberNodes>2)
{
int size = bfs.size();
for(int j = 0; j<size; j++)
{
int temp = bfs.poll();
List<Integer> neighbors = graph[temp];
for(int i = 0; i<neighbors.size(); i++)
{
int node = neighbors.get(i); //1
frequency[node]--;
if(frequency[node]==1)
{
bfs.add(node);
}
}
}
numberNodes-=size;
}
List<Integer> temp = new ArrayList<>(bfs);
return temp;
}
}