Balanced Binary Tree
Problem Statement
You are given the root of a binary tree. Decide whether it is height-balanced.
A tree is height-balanced when, at every node, the heights of its left side and right side differ by at most 1. An empty side has height 0. The rule applies to every node, not just the root.
The root of a binary tree. It can be empty and has at most 5000 nodes.
true if the tree is height-balanced, false otherwise.
At the root, the left side has height 1 (node 9) and the right side has height 2 (node 20). They differ by 1. Node 20 has two leaves, so it differs by 0. Every node passes.
The left side of the root has height 3 (2 → 3 → 4), but the right side is a single node with height 1. They differ by 2, so the tree is not balanced.
An empty tree has no node that fails the rule.
- • The number of nodes is in the range [0, 5000].
- • −10^4 ≤ Node.val ≤ 10^4
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
At each node, measure both sides with separate searches, then check the children the same way
One post-order pass: each node returns its height, or −1 as soon as something is wrong
For every node, call a separate height() on its left child and on its right child. If the difference is more than 1, the tree is not balanced. Otherwise, do the same check on both children.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Tree DFS translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.