Here is the core idea: when you move the root from node to its child , most of the tree stays the same. Only the edge between and changes direction. Nodes in 's subtree get step closer to the new root. Nodes outside 's subtree get step farther.
You do not need to recompute everything from scratch. You adjust the old answer by a small amount that depends on the subtree size of . This incremental update takes per edge. Since a tree has edges, you visit each once and get all answers in total.