Both DFS passes visit each node exactly once. The down pass computes size[v] and down[v] for all n nodes. The up pass computes answer[v] for all n nodes. Each pass is O(n) time.
Total time: O(n) for building the adjacency list, O(n) for the down pass, O(n) for the up pass. That gives O(n) overall.
Space: O(n) for the adjacency list, O(n) for the size, down, and answer arrays. Total space is O(n).
Compare this to the brute force O(n2) approach. For n=200,000, rerooting finishes in milliseconds while brute force would time out.