When counting paths of length through a centroid, you combine distances from different subtrees. For each node at distance from the centroid in subtree A, you count nodes at distance in other subtrees. This avoids double-counting paths within the same subtree.
Then you recurse on each subtree separately. Quiz: If the centroid has children with subtrees of sizes , , and , how many times do you recurse after processing the centroid?