Problems > Diameter of a Tree > Editorial

Diameter of a Tree — Solution & Editorial

Back to the Problem

Double BFS: from any node find the farthest node A; from A find the farthest distance — that is the diameter (provable by the exchange argument on tree paths).

Complexity: O(N)