Tree: (line). Range: , . Centroid is . Distances from : for nodes . For node (distance ), look for distances in range . Since distances are non-negative, this is : nodes and . That is pairs.
For node (distance ), range : all nodes work. Continue this process for all nodes, subtract same-subtree pairs, and sum. The arithmetic gets complex but the pattern is clear.