Binary Tree Height: Let Each Subtree Answer Once
By Hourly DSA · · 3 min read
A binary tree's height describes its longest downward path to a leaf. A recursive solution asks each child for its height, keeps the larger answer, and adds the current node. The main decision is what you count: nodes or edges.
We'll count nodes. An empty tree has height 0, and a leaf has height 1. Stating that convention first prevents an otherwise correct solution from returning an answer that is off by one.
Give every call the same question
Define height(node) as the number of nodes on the longest path from that node down to a leaf. Each call answers that question for its own subtree.
height(node):
if node is null:
return 0
leftHeight = height(node.left)
rightHeight = height(node.right)
return 1 + max(leftHeight, rightHeight)
The caller doesn't need the full paths. It needs one number from each child. That small return value is enough to combine the two answers.
Trace the answers from the leaves
Consider a root A with children B and C. B has a left child D, and D has a left child E. C and E have no children.
The longest path is A → B → D → E, containing four nodes.
E returns 1 because both of its children return 0. D receives 1 from E and 0 from its missing right child, so D returns 2. B returns 3. On the other side, C returns 1. Finally, A returns 1 + max(3, 1) = 4.
The shorter branch still gets visited. You cannot know which branch is taller until you have enough information about both.
Why does taking the maximum work?
Every downward path from a nonempty node begins at that node and then enters at most one child subtree. The longest such path therefore uses the taller child subtree, plus the current node.
Adding both child heights would combine two branches into a path that is no longer a downward root-to-leaf path. That would answer a different question.
This is a useful recursive design habit: explain what the returned value means before deciding how to combine child results. It also helps when defining a dynamic programming state.
Separate height from depth
Depth measures distance from the root to a node. Height measures the longest downward distance from a node to a leaf.
Under the common edge-counting convention, the root has depth 0. In our example, E has depth 3. The whole tree has height 4 under this article's node-counting convention, or height 3 if you count edges instead.
For an edge-counting implementation, returning -1 for an empty subtree makes a leaf return 0. Check the problem's convention before choosing the base case.
Account for the recursion stack
For n nodes, this algorithm takes O(n) time because it visits every node once and does constant work at each visit. Its active recursion stack uses O(h) space, where h is the tree's node-counting height.
A balanced tree has logarithmic height. A chain of n nodes has height n and can exceed a language's recursion limit. The tree shape matters even though the time bound is the same.
This assumes a proper tree: no cycles and no child node shared by multiple parents. Arbitrary pointer graphs need different handling. Our BFS and DFS comparison explains why visited state matters there.
Check one missing child
A root has no left child and one right child, which is a leaf. What height should the root return?
2. Its child heights are 0 and 1. A missing child doesn't end the search through the other branch.
Use Hourly DSA for short reasoning practice. Try stating a recursive function's promise before tracing its calls.
Read next
Use a different recursive promise to check whether every value belongs in its position.