LearnThatStack Ace your next interview
Trees, BSTs & Heaps · question
Question 2 of 76

What is the difference between a node's height and its depth?

beginner
← All Trees, BSTs & Heaps questions
Re-explain
Visual ↓

Depth counts edges from the root down to a node, so the root has depth zero. Height counts edges on the longest path from a node down to a leaf, so a leaf has height zero.

So depth looks upward toward the root, and height looks downward toward the deepest descendant. The whole tree's height equals the root's height, which is the longest root-to-leaf path. That number drives cost, because search and insert run in time proportional to height.

A short height means fast operations. When people say a tree is O(log n), they mean its height stays near log n instead of growing linearly.

A common mix-up is swapping the two or counting nodes instead of edges. Some texts count nodes, making both values larger by one, so always confirm the convention.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for Trees, BSTs & Heaps One question at a time. Answer out loud, get graded, see what you missed.
See it - interactive diagram
Depth and height on every node of one tree
  • Root ADepth is 0. Height is 3, the longest way down.
    • Node BDepth is 1, one edge up. Height is 2, two down.
      • Leaf DDepth is 2. Height is 0 with nothing below it.
      • Node EDepth is 2. Height is 1, one edge down to F.
        • Leaf FDepth is 3, the deepest. Height is 0.
    • Leaf CDepth is 1, the same as B. Height is 0.

Depth counts edges up to the root. Height counts edges down to the deepest leaf. The root has depth 0 and every leaf has height 0.

© LearnThatStack - diagrams may not be republished without permission.

Other questions have diagrams like this too. Sign in to ask for one where it's missing - it's free.

Create a free account
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • How would you compute a tree's height in code, and how does that differ from finding a node's depth? Height is post-order, 1 + the larger child height, with an empty tree at -1 so a leaf is 0. Depth is passed down from the parent.
  • What is the most nodes a binary tree of height h can hold, and what does that say about minimum height? Each level at most doubles, giving 2^(h+1) - 1 nodes. Turned around, n nodes need a height of at least floor(log2 n).
  • What input order pushes a BST's height to n - 1, and what does that do to search? Sorted or reverse-sorted inserts build a chain, so height becomes n - 1 and search drops to O(n). Name self-balancing trees as the fix.
  • How would you check whether a binary tree is height-balanced? Compute heights bottom-up and compare each node's subtrees in the same pass. Calling height() separately at every node can cost O(n^2).

What you can say

  1. Depth is how far a node sits below the root, and height is how far it sits above its deepest leaf.
  2. Both count edges, so the root has depth zero and a leaf has height zero.
  3. The height of the whole tree is the root's height, which is the longest path from the root down to a leaf.
  4. That number sets the cost, because search and insert in a BST take time proportional to height, so O(log n) really means the height stays near log n.
  5. One caution is that some textbooks count nodes instead of edges, which adds one to both numbers, so I'd confirm the convention before giving a number.

Weak answers to avoid

  • Swaps height and depth Calling the root's depth its height is the classic slip. Depth runs from the root, so the root is 0. Height runs down to a leaf, so a leaf is 0.
  • Counts nodes without saying so The answers come out one higher than the edge count most interviewers expect. Say which convention you use, and note that the other adds one.
  • Stops at definitions, never mentions cost Height matters because search and insert cost O(height). Tie it to why balanced trees keep height near log n.
  • Says a BST is always O(log n) Only if height stays near log n. A tree built from sorted input becomes a chain with height n - 1, and every operation goes linear.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Trees, BSTs & Heaps cheatsheet.

← Back to all Trees, BSTs & Heaps questions
Pro · $10/mo

65 of 76 Trees, BSTs & Heaps answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime