If , the tree is a single node with no edges. The answer for that node is because the sum of distances to itself is . No DFS needed, return . If , the tree is a single edge connecting two nodes. Answer for each node is because the distance to the other node is .
Down pass computes this immediately. Always test these small cases. They often reveal off-by-one errors in your recurrence formula, especially in the up[v] calculation where you add or subtract sizes.