File tree 1 file changed +6
-3
lines changed
src/main/java/com/williamfiset/algorithms/graphtheory/treealgorithms
1 file changed +6
-3
lines changed Original file line number Diff line number Diff line change @@ -23,13 +23,15 @@ public static List<Integer> findTreeCenters(List<List<Integer>> tree) {
23
23
for (int i = 0 ; i < n ; i ++) {
24
24
List <Integer > edges = tree .get (i );
25
25
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
+ }
27
30
}
28
31
29
32
int processedLeafs = leaves .size ();
30
33
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
33
35
// until only the centers remain.
34
36
while (processedLeafs < n ) {
35
37
List <Integer > newLeaves = new ArrayList <>();
@@ -39,6 +41,7 @@ public static List<Integer> findTreeCenters(List<List<Integer>> tree) {
39
41
newLeaves .add (neighbor );
40
42
}
41
43
}
44
+ degrees [node ] = 0 ;
42
45
}
43
46
processedLeafs += newLeaves .size ();
44
47
leaves = newLeaves ;
You can’t perform that action at this time.
0 commit comments