Maximum Depth of Binary Tree
Easy
Topics
Given the root of a binary tree, return its maximum depth — the number of nodes along the longest path from the root down to the farthest leaf. Trees are given in level order with null for missing children.
Example 1
Depth counts nodes on a root-to-leaf path, not edges, so the shaded path of 3 nodes is the answer. Each node’s depth is one plus the deeper of its two children, which is a single post-order pass.
Tree given in level-order (breadth-first);
null marks a missing child.Input: root = [3,9,20,null,null,15,7] Output: 3
Example 2
Input: root = [1,null,2] Output: 2
Constraints
- 0 <= number of nodes <= 10^4
- -100 <= Node.val <= 100
Editorial — intuition, approach, complexity and pitfalls
Intuition
The depth of a tree is 1 (for the root) plus the depth of its deeper subtree. That recursive definition is the whole solution.
Approach
- If the node is null, its depth is 0.
- Otherwise return
1 + max(depth(left), depth(right)).
Dry Run
tree 3 9 20 null null 15 7
3
/ \
9 20
/ \
15 7
Depths are computed on the way back up, so a parent reads two finished numbers:
node 9 max(0, 0) + 1 = 1
node 15 max(0, 0) + 1 = 1
node 7 max(0, 0) + 1 = 1
node 20 max(1, 1) + 1 = 2
node 3 max(1, 2) + 1 = 3
answer 3
Node 3 takes the deeper side, 2 rather than 1, and adds itself. The `1 +` is
exactly "and me": strip it and every return reduces to max(0, 0), so the whole
tree reports 0 -- including a single-node tree, which should be 1.
Depth here counts NODES, not edges, which is where most off-by-ones on this
problem come from. The path 3 -> 20 -> 15 has 2 edges and depth 3. Compare
diameter-of-binary-tree, which counts edges instead, and note that the two
problems therefore disagree about the same path on purpose.Complexity
- Time — O(n): every node is visited once.
- Space — O(h): recursion stack up to the tree height h (O(n) worst case for a skewed tree).
Common Pitfalls
- The
1 +is the node itself. Returning onlymax(left, right)never counts anything, so every tree reports depth 0. - Depth counts nodes, not edges. A single-node tree is depth 1, which is the boundary most off-by-ones here come from.
Full Solution — all languages
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def maxDepth(self, root):
if not root:
return 0
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))import java.util.*;
class TreeNode {
int val;
TreeNode left, right;
TreeNode(int v) {
val = v;
}
}
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) return 0;
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
}#include <bits/stdc++.h>
using namespace std;
struct TreeNode {
int val;
TreeNode *left, *right;
TreeNode(int v): val(v), left(nullptr), right(nullptr) {
}
};
class Solution {
public: int maxDepth(TreeNode* root) {
if (!root) return 0;
return 1 + max(maxDepth(root->left), maxDepth(root->right));
}
};/**
* Tree node shape: { val, left, right }
* @param {TreeNode} root
* @return {number}
*/ var maxDepth = function(root) {
if (!root) return 0;
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
};