4165. Find Diameter Endpoints of a Tree

Medium
Tree
Breadth-First Search
Graph Theory

Description

Hints

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

Statistics

Acceptance
69.2%
Submissions
812
Accepted
562