Check Whether a Comment Thread Tree Is Height-Balanced
Last updated: November 29, 2026
Quick Overview
Given the root of a binary tree representing a reply thread, return whether for every node the heights of its left and right subtrees differ by at most one.
Meta
November 29, 20260
0
0 solved
Given the root of a binary tree representing a reply thread, return whether for every node the heights of its left and right subtrees differ by at most one.
A Meta phone screen classic where the naive solution recomputes heights at every node for O(n^2). The interviewer wants a single post-order pass that returns height and balance together, often using a sentinel value.
What the Interviewer Expects
- Define balanced at every node, not just the root
- Avoid repeated height calls
- Return a sentinel like -1 to signal imbalance
- Short-circuit once imbalance is found
- State O(n) time and O(h) space
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you return the first unbalanced node?
- How would you make it iterative?
- How do self-balancing trees like AVL keep this property after inserts?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input is the root of a binary tree or None. A tree is height-balanced when, at every node, the left and right subtree heights differ by at most 1. Return a boolean. An empty tree is balanced....
High-Level Design
Compute heights bottom-up. A helper returns the subtree's height if it is balanced, or -1 if any node inside it is not. At a node, get the left result; if it is -1, propagate immediately. Do the same ...