12. Maximum Depth of Binary Tree
easyAsked at SalesforceReturn the maximum depth (number of nodes along the longest root-to-leaf path) of a binary tree. Salesforce asks this to verify clean recursion — relevant to their role-hierarchy depth queries.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Salesforce loops.
- Glassdoor (2026-Q1)— Used on Salesforce backend phone screens as a tree-recursion baseline.
- Blind (2025-08)— Asked alongside role-hierarchy depth limit discussions.
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
Level-order traversal; count the number of levels.
- Time
- O(n)
- Space
- O(w) where w is max width
function maxDepth(root) {
if (!root) return 0;
const queue = [root];
let depth = 0;
while (queue.length) {
const next = [];
for (const node of queue) {
if (node.left) next.push(node.left);
if (node.right) next.push(node.right);
}
queue.splice(0, queue.length, ...next);
depth++;
}
return depth;
}Tradeoff: Works but verbose. Use when you also need level-by-level data; otherwise prefer DFS.
2. Recursive DFS
Depth = 1 + max(depth of left, depth of 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: Three lines of clean recursion. The base case (null = 0) makes the recursion compose correctly.
Salesforce-specific tips
Salesforce role-hierarchy has a documented depth limit (8 levels in production), so they grade this problem on whether your solution scales to that — both versions do, but the DFS is what they expect. Bonus signal: mention iterative DFS with explicit stack when asked about avoiding recursion limits.
Common mistakes
- Returning Math.max(maxDepth(left), maxDepth(right)) without +1 — gives depth - 1.
- Returning 1 for null instead of 0 — gives off-by-one.
- Forgetting the null check at root entry — crashes on empty tree.
Follow-up questions
An interviewer at Salesforce may pivot to one of these next:
- Minimum Depth of Binary Tree (LC 111).
- Balanced Binary Tree (LC 110) — uses max depth as a subroutine.
- Diameter of Binary Tree (LC 543) — pairs depth tracking with global max.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is the recursion 3 lines but BFS is 12?
DFS leverages the call stack as an implicit recursion structure; BFS needs explicit queue management. For pure depth, the explicit structure is overhead.
What if the tree is a million nodes deep (skewed)?
The recursive DFS overflows the call stack. Switch to iterative DFS with an explicit stack, or use Morris-style traversal.
Practice these live with InterviewChamp.AI
Drill Maximum Depth of Binary Tree and other Salesforce interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →