Reproduce behaviour of getDepthInTree exactly.

However, it does not seem to be needed. For safety only, as this
does not slow anything down.