You are given a tree with nodes. For each node, calculate the maximum distance from that node to any other node. This extends diameter to all nodes. Naive approach: run BFS from every node, .
Better approach: use the diameter endpoints. For any node , its farthest node is one of the diameter endpoints. Run BFS from both endpoints, then answer for each node is max of the two distances.
Space complexity is for the data structures used.