12. Maximum Depth of Binary Tree
easyAsked at SquareReturn the maximum depth of a binary tree. Square uses this as a warm-up to see whether you can write a clean post-order recursion before tackling harder tree problems like balance and path-sum.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Square loops.
- Glassdoor (2025)— Square POS phone screen.
- LeetCode Discuss (2026)— Cash App backend warm-up.
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. Recursive DFS
depth(node) = 1 + max(depth(left), depth(right)); null = 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: Clean and idiomatic. Stack depth = tree height — risky for adversarial inputs.
2. Iterative BFS with level count
Process the tree one level at a time, incrementing a counter.
- Time
- O(n)
- Space
- O(w)
function maxDepth(root) {
if (!root) return 0;
let q = [root];
let depth = 0;
while (q.length) {
const next = [];
for (const node of q) {
if (node.left) next.push(node.left);
if (node.right) next.push(node.right);
}
q = next;
depth++;
}
return depth;
}Tradeoff: Iterative — safer for deep trees. Swapping queues avoids the shift() O(n) trap.
Square-specific tips
Square interviewers grade this on whether your recursion has a clear base case and a single recursive step. Mention which version you'd ship in production (iterative for unbounded trees) and why — they care about defensive coding for transaction-graph traversals.
Common mistakes
- Counting edges instead of nodes — the problem asks for node-count along the longest path.
- Initializing depth = 1 even for null root (should be 0).
- Using array.shift() inside the BFS loop — O(n^2) total.
Follow-up questions
An interviewer at Square may pivot to one of these next:
- Minimum depth of a binary tree (LC 111) — careful, must reach a leaf.
- Diameter of binary tree (LC 543).
- Balanced binary tree (LC 110).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Is depth measured in nodes or edges?
Nodes per this problem's definition. Some textbooks use edges; always clarify with the interviewer.
Why is recursion's space O(h), not O(n)?
The call stack only holds the current path from root to the deepest active node at any moment — that's the tree's height.
Practice these live with InterviewChamp.AI
Drill Maximum Depth of Binary Tree and other Square interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →