File tree Expand file tree Collapse file tree 1 file changed +6
-3
lines changed
src/main/java/com/williamfiset/algorithms/graphtheory/treealgorithms Expand file tree Collapse file tree 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) {
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 ;
You can’t perform that action at this time.
0 commit comments