juniorTrees

How do you find the height and depth of a binary tree?

Updated Apr 28, 2026

Short answer

The height of a binary tree is the number of edges on the longest path from the root to any leaf, while the depth of a node is the number of edges from the root to that node. To find the tree height, use recursion: the height is 1 + max(left subtree height, right subtree height), with an empty tree having height -1 (or 0 depending on the chosen definition). The maximum depth of a tree is the same value as its height when measured from the root.

Deep explanation

Height and depth are closely related but describe different directions of measurement:

  • Depth of a node:
  • Measures how far a node is from the root.
  • The root has depth 0.
  • A child of the root has depth 1.
  • It is calculated while moving downward from the root.
  • Height of a node:
  • Measures the longest path from that node to a leaf.
  • A leaf node has height 0.
  • It is calculated while moving upward after visiting children.

For the entire binary tree:

  • The tree's height is the height of the root node.
  • The tree's maximum depth is the deepest node's depth.
  • These two values are equal when both use the same unit (usually number of edges).

Recursive approach for height

The recursive idea is:

  1. If the current node is null, return the base height.
  2. Find the height of the left subtree.
  3. Find the height of the right subtree.
  4. Return one more than the larger subtree height.

Example:

TEXT
1
/ \
2 3
/
4

The heights are:

  • Node 4: height = 0
  • Node 2: height = 1
  • Node 3: height = 0
  • Node 1: height = 2

So the binary tree height is 2.

Recursive implementation

Python
def height(root):
if root is None:
return -1 # Height measured using edges
left_height = height(root.left)
right_height = height(root.right)
return 1 + max(left_height, right_height)

If using the definition where height is measured by number of nodes instead of edges, the base case changes:

Python
def height(root):
if root is None:
return 0
return 1 + max(height(root.left), height(root.right))

The algorithm remains the same; only the definition changes.

Finding depth of a particular node

To find the depth of a node, start from the root and count the number of edges traveled:

Python
def find_depth(root, target, depth=0):
if root is None:
return -1
if root.value == target:
return depth
left = find_depth(root.left, target, depth + 1)
if left != -1:
return left
return find_depth(root.right, target, depth + 1)

Iterative approach using BFS

A level-order traversal can also find height by counting levels:

Python
from collections import deque
def height(root):
if root is None:
return -1
queue = deque([root])
height = -1
while queue:
level_size = len(queue)
height += 1
for _ in range(level_size):
node = queue.popleft()
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return height

Complexity analysis

For both DFS recursion and BFS:

  • Time complexity: O(n)
  • Every node is visited once.
  • Space complexity:
  • Recursive DFS: O(h) for the call stack, where h is tree height.
  • BFS: O(w) where w is the maximum width of the tree.

DFS is usually preferred for finding height because the problem naturally follows the recursive tree structure.

Real-world example

A common use case is finding the height of a folder structure. A computer directory tree is similar to a binary tree: folders contain subfolders, and the height represents the maximum number of nested levels.

For example, a file system monitoring tool might calculate the deepest folder nesting:

Python
class Folder:
def __init__(self, name):
self.name = name
self.left = None
self.right = None
def folder_depth(folder):
if folder is None:
return -1
return 1 + max(
folder_depth(folder.left),
folder_depth(folder.right)
)
root = Folder("Documents")
root.left = Folder("Projects")
root.left.left = Folder("InterviewPrep")
print(folder_depth(root)) # Output: 2

The result means the deepest folder is two levels below the root folder.

Common mistakes

  • * Confusing height and depth by using the terms interchangeably without specifying the direction of measurement.
  • * Forgetting to define whether height is measured in edges or nodes.
  • * Returning `0` for an empty tree when the problem expects height measured in edges (`-1` is often used).
  • * Using only one subtree when calculating height instead of taking the maximum of both left and right subtrees.
  • * Forgetting the base case for an empty tree, which can cause recursion errors.
  • * Assuming a balanced tree and ignoring the possibility of a highly skewed tree.
  • * Using BFS when a simple recursive DFS solution would be clearer and more efficient.

Follow-up questions

  • What is the difference between height and depth in a binary tree?
  • How would you find the maximum depth of a binary tree?
  • What is the time and space complexity of finding tree height?
  • How would you find the height of a binary tree without recursion?
  • What happens if the binary tree is completely skewed?
  • Can height be used to determine if a binary tree is balanced?

More Trees interview questions

View all →