Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Do a DFS from the root. At each node, calculate the longest path we can make from two branches of that subtree.
To do that, we need to find the length of the longest path from each of the node’s children.