If you root the tree at node , you can compute the sum of distances from node to all others using DFS. That is one answer.
Now how do you compute the answer for node (a child of )? When you move the root from to , some nodes get closer and some get farther.
Nodes in the subtree of get closer. Nodes outside the subtree of get farther. You can update the sum in using this observation.
This is called rerooting DP.