Graph Theory37 sections · 1633 units
Open in Course

CSES 1132 Tree Distances I - Walkthrough

Example trace

Tree: edges (11,22), (11,33), (33,44), (33,55). BFS from 11 gives farthest nodes 44 and 55 (distance 22). Pick 44.

BFS from 44 gives farthest node 22 (distance 33). BFS from 22 gives distances {22:00, 11:11, 33:22, 44:33, 55:33}.

From endpoint 44: distances {44:00, 33:11, 55:22, 11:22, 22:33}. From endpoint 22: distances {22:00, 11:11, 33:22, 44:33, 55:33}.

For node 33: max(dist from 44, dist from 22) = max(11, 22) = 22. That is node 33's answer.