13. Balanced Binary Tree
easyAsked at VercelDetermine if a binary tree is height-balanced (left and right subtrees of every node differ in height by at most 1). Vercel asks this for the early-exit recursive pattern — a small twist on max-depth that catches sloppy candidates.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Vercel loops.
- Glassdoor (2025-12)— Vercel screen; the early-exit version is the bonus signal.
- Blind (2026-Q1)— Mentioned in Vercel onsite recap.
Problem
Given a binary tree, determine if it is height-balanced. A height-balanced binary tree is defined as a binary tree in which the left and right subtrees of every node differ in height by no more than 1.
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]falseApproaches
1. Compute height at every node
For every node, compute height of left and right; compare. Recurse into both children.
- Time
- O(n^2) worst case
- Space
- O(h)
function isBalanced(root) {
if (!root) return true;
const height = (n) => !n ? 0 : 1 + Math.max(height(n.left), height(n.right));
return Math.abs(height(root.left) - height(root.right)) <= 1
&& isBalanced(root.left)
&& isBalanced(root.right);
}Tradeoff: Quadratic because height() recurses fully at every node. Easy to write but slow.
2. Single-pass with early-exit sentinel (optimal)
Return height if balanced, -1 if not. As soon as any subtree is unbalanced, the -1 propagates up and short-circuits.
- Time
- O(n)
- Space
- O(h)
function isBalanced(root) {
function check(node) {
if (!node) return 0;
const l = check(node.left);
if (l === -1) return -1;
const r = check(node.right);
if (r === -1) return -1;
if (Math.abs(l - r) > 1) return -1;
return 1 + Math.max(l, r);
}
return check(root) !== -1;
}Tradeoff: O(n) single pass. The sentinel value (-1) lets one return value carry both 'height' and 'this subtree is bad' — a common interview pattern.
Vercel-specific tips
Vercel grades for the early-exit pattern. Writing the O(n^2) version first is fine — say 'this is the naive approach, let me show you how to fold it into one pass.' Bonus signal: explaining why -1 is a safe sentinel (heights are non-negative).
Common mistakes
- Returning the height with no sentinel — forces you back to the O(n^2) approach.
- Forgetting to check both children's sentinels before computing the local height.
- Confusing 'balanced' with 'complete' or 'perfect' — those are different definitions.
Follow-up questions
An interviewer at Vercel may pivot to one of these next:
- Self-balancing tree insertion (AVL or red-black).
- Convert sorted array to balanced BST (LC 108).
- Maximum depth (LC 104) — base case for this problem.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is -1 a safe sentinel?
Heights are >= 0 by definition. Using -1 as 'this subtree is unbalanced' carves out an unreachable value that's cheap to test for.
What's the difference between balanced and complete?
Balanced is about height: every node's subtrees differ by at most 1. Complete is about shape: every level except possibly the last is full, and the last is filled left-to-right. They're orthogonal.
Practice these live with InterviewChamp.AI
Drill Balanced Binary Tree and other Vercel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →