Node Depth and Height
You'll learn how to measure vertical distances in a tree: depth (from root downward) and height (from leaves upward).
Now that you understand the tree structure—roots, parents, children, paths, ancestors, and descendants—you need ways to measure how "far" nodes are from key reference points. Two fundamental measurements answer this: depth tells you how far down a node sits from the root, while height tells you how far up from the deepest leaf below.
Node Depth
Depth measures the distance from the root to a specific node. It's the number of edges you must traverse along the unique path from root to that node.
- The root itself has depth 0 (it's already at the top)
- The root's immediate children have depth 1
- Their children have depth 2, and so on
Think of depth like floors in a building counted from ground level: the lobby is floor 0, first floor is 1, etc.
Node Height
Height measures the distance from a node down to its deepest descendant leaf. It's the longest path from that node to any leaf below it.
- Any leaf has height 0 (no descendants below)
- A parent of leaves has height 1
- The root's height equals the tree's overall height
Think of height like measuring a person by standing them on the ground—you measure from their feet (leaves) upward to their head (the node in question).
The Tree's Height
The height of the entire tree is simply the height of the root node—the longest path from root to any leaf.
Key Takeaway:
Depth measures distance down from the root to a node (root = 0); height measures distance up from a node to its deepest leaf (leaves = 0). Both count edges, not nodes.