Ezvor

3787. Find Diameter Endpoints of a Tree

MediumTree Preparing judge… LeetCode
TreeBreadth-First SearchGraph Theory

Hints

Hint 1
Start a breadth first search (BFS) from any node start; let the farthest node found be A.
Hint 2
Run BFS from A; a farthest node B is at the other end, and the path A-B is a diameter.
Hint 3
If several nodes tie as farthest from start, collect them into cand_start, each is a possible diameter endpoint.
Hint 4
Running BFS from any node in cand_start yields the opposite-end set cand_other, the other diameter endpoints.