You could run BFS from every node, find the farthest distance from each, and take the maximum. That gives you the diameter but costs time: BFS calls, each taking .
For small trees this works. For , you would do operations, which is too slow. You need a smarter approach that finds the diameter without checking every starting point. The two-BFS method does exactly this in total time. It exploits a property of trees that general graphs lack.
Space complexity is for the data structures used.