Skip to content

Commit 7e98e2f

Browse files
committed
Merge branch 'master' of github.com:williamfiset/Algorithms
2 parents 7dfd917 + 2fe0712 commit 7e98e2f

File tree

1 file changed

+6
-3
lines changed
  • src/main/java/com/williamfiset/algorithms/graphtheory/treealgorithms

1 file changed

+6
-3
lines changed

src/main/java/com/williamfiset/algorithms/graphtheory/treealgorithms/TreeCenter.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ public static List<Integer> findTreeCenters(List<List<Integer>> tree) {
2323
for (int i = 0; i < n; i++) {
2424
List<Integer> edges = tree.get(i);
2525
degrees[i] = edges.size();
26-
if (degrees[i] <= 1) leaves.add(i);
26+
if (degrees[i] <= 1) {
27+
leaves.add(i);
28+
degrees[i] = 0;
29+
}
2730
}
2831

2932
int processedLeafs = leaves.size();
3033

31-
// Remove leaf nodes and decrease the degree of
32-
// each node adding new leaf nodes progressively
34+
// Remove leaf nodes and decrease the degree of each node adding new leaf nodes progressively
3335
// until only the centers remain.
3436
while (processedLeafs < n) {
3537
List<Integer> newLeaves = new ArrayList<>();
@@ -39,6 +41,7 @@ public static List<Integer> findTreeCenters(List<List<Integer>> tree) {
3941
newLeaves.add(neighbor);
4042
}
4143
}
44+
degrees[node] = 0;
4245
}
4346
processedLeafs += newLeaves.size();
4447
leaves = newLeaves;

0 commit comments

Comments
 (0)