Classic problem, the kind you've either drilled or you haven't.
Clarify that the tree is unweighted and then present a linear-time solution using two BFS/DFS traversals: first from any node to find the farthest node A, then from A to find the farthest node B, where the distance between A and B is the diameter. Alternatively, mention a single DFS that computes the diameter by combining the two longest downward paths at each node.
Pro tip: Emphasize that the two-BFS approach works only for trees (or unweighted graphs) and that for weighted trees you need the DP approach; also discuss handling edge cases like a single node or empty tree.
Confirm whether the tree is unweighted, whether it's a binary tree or general tree, and how the input is represented (adjacency list, edges, etc.).
Describe the algorithm: pick an arbitrary node, run BFS to find the farthest node A, then run BFS from A to find the farthest node B; the distance from A to B is the diameter.
Briefly justify why the first BFS finds an endpoint of the diameter: if the farthest node from an arbitrary node is not an endpoint, a longer path can be constructed, leading to a contradiction.
State that the algorithm runs in O(N) time and O(N) space, where N is the number of nodes, since each BFS visits all nodes and edges once.
Mention the single DFS DP approach for weighted trees or if recursion is preferred, and cover edge cases such as a single node (diameter 0) or empty tree.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.