14. Balanced Binary Tree
easyAsked at RedditDetermine if a binary tree is height-balanced. Reddit uses this to test bottom-up recursion — the same insight that lets them detect comment subtrees that have grown pathologically deep on one side (a fingerprint of bot reply chains).
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Reddit loops.
- Glassdoor (2026-Q1)— Reddit phone screen, common before fraud-detection follow-ups.
Problem
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differs by more than one.
Constraints
The number of nodes in the tree is in the range [0, 5000].-10^4 <= Node.val <= 10^4
Examples
Example 1
root = [3,9,20,null,null,15,7]trueExample 2
root = [1,2,2,3,3,null,null,4,4]falseExample 3
root = []trueApproaches
1. Top-down (recompute depth per node)
For each node, compute left depth and right depth via maxDepth; check difference.
- Time
- O(n^2)
- Space
- O(h)
function maxDepth(n) {
if (!n) return 0;
return 1 + Math.max(maxDepth(n.left), maxDepth(n.right));
}
function isBalanced(root) {
if (!root) return true;
const l = maxDepth(root.left), r = maxDepth(root.right);
if (Math.abs(l - r) > 1) return false;
return isBalanced(root.left) && isBalanced(root.right);
}Tradeoff: Recomputes depths repeatedly. O(n^2) in the worst case (a left-leaning chain).
2. Bottom-up height with sentinel (optimal)
Return -1 from any unbalanced subtree to short-circuit. Otherwise return the actual height.
- Time
- O(n)
- Space
- O(h)
function isBalanced(root) {
function height(node) {
if (!node) return 0;
const l = height(node.left);
if (l === -1) return -1;
const r = height(node.right);
if (r === -1) return -1;
if (Math.abs(l - r) > 1) return -1;
return 1 + Math.max(l, r);
}
return height(root) !== -1;
}Tradeoff: Linear time, single pass. The -1 sentinel encodes 'unbalanced' so we don't need a second return value.
Reddit-specific tips
Reddit interviewers grade on whether you avoid the O(n^2) top-down trap. Bonus signal: connect this to detecting one-sided reply chains — bot accounts often spawn deep one-sided subtrees that trip a height-balance heuristic in their abuse pipeline.
Common mistakes
- Recomputing depth at every node (O(n^2) trap).
- Forgetting to propagate the -1 sentinel up.
- Returning false instead of -1 inside the helper — loses the type contract.
Follow-up questions
An interviewer at Reddit may pivot to one of these next:
- Diameter of binary tree (LC 543) — same bottom-up pattern.
- Binary tree maximum path sum (LC 124) — extends the pattern with a global accumulator.
- What if 'balanced' means weight (size) instead of height?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why return -1 instead of throwing?
Throws are expensive in JS and obscure control flow. -1 as a sentinel is idiomatic for 'height can't be negative'.
Does this work for n-ary trees?
Generalize the height function to take max over children[]. Same -1 short-circuit applies.
Practice these live with InterviewChamp.AI
Drill Balanced Binary Tree and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →