Sponsored
Use these hints if you're stuck. Try solving on your own first.
Can we use dp on trees?
Store the two most distant children for each node.
When re-rooting the tree, keep a variable for distance to the root node.
Solve with full IDE support and test cases