Run BFS from node to find one diameter endpoint .
Run BFS from to find the other endpoint .
Run BFS from (or reuse from step ).
For each node : answer = max(dist from , dist from ).
Three BFS calls total: . This exploits the fact that diameter endpoints are the "extreme" nodes. Any other node's farthest point must be one of them.
Space complexity is for the data structures used.