Skip to main content
PreparationZone Logo
PreparationZone
Back to Fundamentals

Fundamentals · Easy · Free

Compute height of a binary tree — recursive definition.

FresherFundamentalsData Structures & Algorithms
Model answer

Tree height

java
int height(TreeNode n) {
  if (n == null) return -1; // or 0 depending on definition
  return 1 + Math.max(height(n.left), height(n.right));
}

Clarify empty tree height 0 vs -1—consistency matters.

Balanced tree height O(log n); skewed O(n).