2dbi

Maximum Height of Binary Tree

viaLeetCode

Problem Return the maximum depth of a binary tree — the number of nodes on the longest root-to-leaf path.

Input / Output

  • Input: tree root. Output: int depth (0 for empty tree).

Constraints

  • Up to 10^4 nodes; O(n) required; recursion depth = tree height (mention skew).

Example

  • [3,9,20,null,null,15,7] → 3.

Expected approach

  • Recursive DFS: depth(node) = 1 + max(depth(left), depth(right)), null → 0 — three lines, O(n)/O(h). Iterative alternatives: BFS counting levels (natural fit), or DFS with explicit stack of (node, depth) for skewed trees. Clarify nodes-vs-edges definition of height — off-by-one convention differs by source. Balanced-tree check and diameter are the standard follow-ons built on the same recursion.
Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account