13. Maximum Depth of Binary Tree
easyAsked at DatadogReturn the maximum depth of a binary tree. Datadog uses this as the simplest height question and then escalates to bounded-memory variants for trees stored on disk in chunked form.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Datadog loops.
- Glassdoor (2026-Q1)— Datadog phone screen tree warmup.
- LeetCode Discuss (2025-10)— Listed in Datadog tagged problems.
Problem
Given the root of a binary tree, return its maximum depth. A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Constraints
The number of nodes in the tree is in the range [0, 10^4].-100 <= Node.val <= 100
Examples
Example 1
root = [3,9,20,null,null,15,7]3Example 2
root = [1,null,2]2Approaches
1. BFS level count
Iterate level by level with a queue; count levels.
- Time
- O(n)
- Space
- O(w)
function maxDepth(root) {
if (!root) return 0;
let depth = 0;
let q = [root];
while (q.length) {
depth++;
const next = [];
for (const n of q) {
if (n.left) next.push(n.left);
if (n.right) next.push(n.right);
}
q = next;
}
return depth;
}Tradeoff: O(w) space (width). Good when the tree is deeper than it is wide.
2. Recursive DFS (optimal for balanced trees)
depth = 1 + max(depth(left), depth(right)). Base case: null returns 0.
- Time
- O(n)
- Space
- O(h)
function maxDepth(root) {
if (!root) return 0;
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}Tradeoff: Three lines. O(h) recursion stack — h can be n in a degenerate (linked-list-shaped) tree.
Datadog-specific tips
Datadog interviewers will follow up with: 'Now compute the depth of a tree stored on disk where each node is a separate read.' Show that BFS minimizes random reads (level-by-level batching), while DFS pipelines well for sequential reads. The tradeoff depends on storage layout.
Common mistakes
- Returning maxDepth(root.left) + maxDepth(root.right) — that's the sum of two subtree heights, not the max plus root.
- Forgetting the +1 for the current node.
- Treating null as 1 instead of 0 — returns depth as if every leaf was at depth 1 above where it actually is.
Follow-up questions
An interviewer at Datadog may pivot to one of these next:
- Balanced Binary Tree (LC 110) — depth + balance check in one pass.
- Minimum Depth (LC 111) — careful: a single-child node is not a leaf.
- Diameter of Binary Tree (LC 543) — depth as a building block.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
DFS or BFS — which does Datadog prefer?
Either is accepted. The follow-up usually pushes toward BFS because their TSDB chunks are read level-by-level, so BFS aligns with the storage layout.
What's the difference between depth and height?
Depth = distance from root. Height = distance to deepest descendant. For the root, depth(root) == height(tree). For internal nodes they differ.
Practice these live with InterviewChamp.AI
Drill Maximum Depth of Binary Tree and other Datadog interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →