Dynamic Programming Problem on Tree
So the difficulty is to determine $dp[u]$, depth of $u$, starting from the list of its sons depths $L[dp[v]]$.

Once $L[dp[v]]$ is sorted in a heap, you can recursively and greedily pop the 2 minimum values $i$, $j$ and push $1+max(i, j)$.

For instance, $L[dp[v]] = [7,5,5,4,3,2,2]$:

=&gt; [7,5,5,4,3,3]
=&gt; [7,5,5,4,4]
=&gt; [7,5,5,5]
=&gt; [7,6,5]
=&gt; [7,7]


$dp[u] = 8$

This is actually $O(nlog(n))$ due to the heap sort.