For each node , let size[v] be the size of its subtree, including itself. You compute this with a single DFS using the formula size[v] = 1 + Σ_u in children[v] size[u]. For a leaf, size[v] = 1 since it has no children. For an internal node, you add for the node itself plus the sizes of all child subtrees.
Subtree sizes determine which edges are heavy. Nodes with large subtrees get heavy edges. This computation takes time, visiting each node once.
Space complexity is for the data structures used.