What is the depth of a node, and how is it computed?
The depth of a node is the number of its ancestors (not counting itself), which equals the number of edges on the path up to the root; the root has depth 0, and every other node has depth 1 + the depth of its parent.
The definition gives a recursive algorithm directly:
Algorithm depth(T, v)
if v is the root of T then
return 0
else
return 1 + depth(T, v.parent())
It walks upwards, one parent at a time, until it reaches the root. So its running time is proportional to the depth itself: $O(d_v + 1)$ for a node at depth $d_v$, and $O(n)$ in the worst case of a tree that is one long chain.
In a document outline, depth corresponds to the heading level: the title has depth 0, the chapters depth 1, the sections depth 2.
Gotcha: depth counts edges, not nodes. A child of the root has one ancestor, so its depth is 1, even though the path from the root to it contains two nodes.