Computing subtree sizes enables quick centroid checks in time per node. Walking toward the heavy subtree guarantees you find a centroid in linear time. No fancy data structures needed. This algorithm is the building block for centroid decomposition. You will call it recursively on smaller components, each time removing the centroid and splitting the tree.
Once you have this, you are ready for path-counting problems. The hard part is understanding how to use centroids, not finding them.