Given a directed, rooted tree with $n$ vertices, the height of a vertex $v$, $h[v]$ is the number of edges on the longest path from $v$ to some reachable leaf node. Give an efficient algorithm to find the following,
$ d[v] := \max\limits_{w \text{ is a sibling of $v$}} h[w] \qquad \forall v \in V. $
Note: The algorithm I was able to come up is the trivial one.
- Finds the height of all vertices using DFS.
- Do a BFS from the root and store all the vertices to a particular distance in a map.
- Go over the list for every element $v$ and find the maximum using the expression above.
Can we do better than this?