Maths WorkshopMaths problems with full solutions
Exercise 967Combinatorics › Graph theoryL1Added 3 Nov 2026

Diameter of a tree

#tree#diameter#BFS

Statement

Show that the diameter of a tree (longest shortest path) can be found by two BFS runs.

Hint

First BFS from any vertex; the farthest vertex is an endpoint of a diameter. Second BFS from there.

Show solutionHide solution

Solution

First BFS. Start from any vertex rr, find farthest vertex uu.

Claim. uu is an endpoint of a diameter.

Second BFS. Start from uu, find farthest vertex vv. Then d(u,v)d(u,v) is the diameter.

Conclusion. Two BFS runs compute the diameter of a tree in O(∣V∣)O(|V|).