Skip to content

Commit db42028

Browse files
refactor 310
1 parent dadf541 commit db42028

File tree

1 file changed

+35
-30
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+35
-30
lines changed

src/main/java/com/fishercoder/solutions/_310.java

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
import java.util.Set;
99

1010
/**
11-
* 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.
11+
* 310. Minimum Height Trees
12+
*
13+
* For a undirected graph with tree characteristics, we can choose any node as the root.
14+
* The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height are called minimum height trees (MHTs).
15+
* Given such a graph, write a function to find all the MHTs and return a list of their root labels.
1216
1317
Format
1418
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).
@@ -19,7 +23,7 @@
1923
2024
Given n = 4, edges = [[1, 0], [1, 2], [1, 3]]
2125
22-
0
26+
0
2327
|
2428
1
2529
/ \
@@ -50,40 +54,41 @@
5054
*/
5155
public class _310 {
5256

53-
public List<Integer> findMinHeightTrees(int n, int[][] edges) {
54-
if (n == 1) {
55-
return Collections.singletonList(0);
56-
}
57+
public static class Solution1 {
58+
public List<Integer> findMinHeightTrees(int n, int[][] edges) {
59+
if (n == 1) {
60+
return Collections.singletonList(0);
61+
}
5762

58-
List<Set<Integer>> adj = new ArrayList<>(n);
59-
for (int i = 0; i < n; ++i) {
60-
adj.add(new HashSet<>());
61-
}
62-
for (int[] edge : edges) {
63-
adj.get(edge[0]).add(edge[1]);
64-
adj.get(edge[1]).add(edge[0]);
65-
}
63+
List<Set<Integer>> adj = new ArrayList<>(n);
64+
for (int i = 0; i < n; ++i) {
65+
adj.add(new HashSet<>());
66+
}
67+
for (int[] edge : edges) {
68+
adj.get(edge[0]).add(edge[1]);
69+
adj.get(edge[1]).add(edge[0]);
70+
}
6671

67-
List<Integer> leaves = new ArrayList<>();
68-
for (int i = 0; i < n; ++i) {
69-
if (adj.get(i).size() == 1) {
70-
leaves.add(i);
72+
List<Integer> leaves = new ArrayList<>();
73+
for (int i = 0; i < n; ++i) {
74+
if (adj.get(i).size() == 1) {
75+
leaves.add(i);
76+
}
7177
}
72-
}
7378

74-
while (n > 2) {
75-
n -= leaves.size();
76-
List<Integer> newLeaves = new ArrayList<>();
77-
for (int i : leaves) {
78-
int j = adj.get(i).iterator().next();
79-
adj.get(j).remove(i);
80-
if (adj.get(j).size() == 1) {
81-
newLeaves.add(j);
79+
while (n > 2) {
80+
n -= leaves.size();
81+
List<Integer> newLeaves = new ArrayList<>();
82+
for (int i : leaves) {
83+
int j = adj.get(i).iterator().next();
84+
adj.get(j).remove(i);
85+
if (adj.get(j).size() == 1) {
86+
newLeaves.add(j);
87+
}
8288
}
89+
leaves = newLeaves;
8390
}
84-
leaves = newLeaves;
91+
return leaves;
8592
}
86-
return leaves;
8793
}
88-
8994
}

0 commit comments

Comments
 (0)