Sum of Distances to All Nodes in a Tree

Asked at Google · Coding Questions · Difficulty: Hard · Last asked Jan 19, 2026

Given a tree, find for every node the total distance from that node to all the other nodes. This is the quantity used to locate the most central node of a network, for example the best site for a hub that minimises the total travel to all other sites.

Loading the problem…