Help for a codeforces Problem

This was a problem from the recent global round of codeforces . It would be very help ful if some one explains me the logic behind solving the problem . Problem Link Thanks in advance

Dissociate the entire tree into subtrees of height atmost 2 and then merge them optimally 2 at a time to form a single component again.