Course contentsShow
← Computer Science
Lesson 483 of 2,87211. Binary TreesFree lesson

Tree Height and Depth

Understanding height (distance to farthest leaf) and depth (distance from root) as fundamental tree metrics.

Tree Height and Depth

What you'll learn: How to measure a tree's vertical dimensions using height and depth—two fundamental metrics for understanding tree structure.

The Core Concepts

In trees, we measure two related but distinct vertical distances:

Depth measures how far down a node is from the root. It's the number of edges on the path from the root to that node. The root itself has depth 0, its children have depth 1, their children have depth 2, and so on.

Height measures how far up you can go from a node to its farthest descendant leaf. A leaf node has height 0 (nowhere to go), and a node's height is 1 plus the maximum height of its children. The height of the entire tree is the height of the root node.

The Family Tree Analogy

Think of a family tree: your depth is how many generations you are from the ancestor at the top. Your height is how many generations below you extend to your most distant descendant.

Why These Matter

These metrics tell us:

  • How "tall" the tree is (affects traversal time)
  • How balanced the tree is (height reveals skewness)
  • Which level a node occupies (important for level-order traversal)
  • Performance characteristics (deeper trees mean longer searches)

For a node at the bottom-left leaf of a tree, its depth might be 3 (three edges from root), while its height is 0 (it's a leaf). Meanwhile, the root has depth 0 but might have height 3 (three edges to the farthest leaf).

Key Takeaway: Depth measures distance from the root downward; height measures distance to the farthest leaf upward. Both are counted in edges, and together they describe a tree's vertical structure.