For each node , the final answer combines two pieces of information.
The down contribution is what you computed when was in a subtree during Phase . This captures everything below .
The up contribution is what comes from the rest of the tree when acts as the root during Phase . This captures everything above .
For tree diameter, the answer at node is the maximum depth you can reach going down into any subtree OR going up through the parent and then down into sibling subtrees.
answer[v] := max(dp_down[v], dp_up[v])
This gives you every node's "view" of the tree. You compute all answers in total time instead of running separate DFS traversals. That is the power of rerooting: you reuse information instead of recomputing it.
Space complexity is for the data structures used.