When implementing for LeetCode (-indexed), root the tree at node instead of node . Your adjacency list uses indices through . The down pass DFS starts at dfs1(0, -1) and the up pass at dfs2(0, -1).
In the up pass, the formula stays: answer[v] = answer[u] + n - 2 * size[v]. No prefix-suffix arrays needed since sum is invertible (you subtract instead of needing to exclude).
Watch for the base case: size[v] = 1 for leaf nodes, down[v] = 0 for leaves. Initialize both arrays to zero before starting.