LeetCode is the same problem as CSES Tree Distances II with a different interface. You get an integer and an array of edges. Return an array where answer[i] is the sum of distances from node to all other nodes.
The difference from CSES: LeetCode uses -indexed nodes (nodes through ), while CSES uses -indexed. Your rerooting logic stays identical. You only change the loop bounds and array indexing.
Try implementing the two-pass solution. Root at node , compute subtree sizes and distance sums in the down pass, then reroot in the up pass.