Select an arbitrary node v in the tree T. Start BFS by creating v as the root of T. BFS displays the distances from v to all other nodes of T.
Now select node u, which is further from v. Run BFS again, creating u as the root. At the output of the new distance, find node w, which is further from u.
u w.
T.
node T.
, . , .
: O (n), n - T.
: (u), node v, .
, , u, , , DFS, .
: retucto ad absurdum. , u --- r - ; node v v --- u, v --- r - v. v --- k. :
a) u --- r v - k node o. v - o - u v - o - r u --- o --- k. o --- r o --- k. u --- o --- r , u --- o --- k . .
b) u --- r v - k . , o1 o2, o1 - o2 . , ), 1 - 2 ( , 1 = 2).
, , .
( , , ).