Pick a random node i.
i
DFS to find the furthest node j to node i.
j
DFS to find the furthest node k to node j.
k
The distance between j and k is the tree's diameter.
See proofarrow-up-right
1617. Count Subtrees With Max Distance Between Cities (Hard)arrow-up-right
Last updated 5 years ago