Skip to main content

How are the height and depth of a tree related?

Height and depth of a tree are related but oppositely directed characteristics.

Depth

Measured downward from the root. Shows how far a specific node is from the root.

javascript
depth(node) = number of edges from the root to this node

Height

Measured upward from a leaf. Shows how many edges lie between a node and the deepest leaf.

javascript
height(node) = length of the longest path from this node to a leaf

Relationship

  • The root's depth = 0, the tree's height = the root's height.
  • The depth of the deepest node = the tree's height.

Example

javascript
A / \ B C / \ D E
  • Depth of A = 0, B = 1, D = 2
  • Tree height = 2 The maximum depth of a tree equals its height.

Summary

Depth is the path down to a node, height is the path up from a node. Maximum depth = the height of the whole tree.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.