You are given the root of a binary tree. The tree is height-balanced when, for every node, the heights of its left and right subtrees differ by no more than one.
Return true if the whole tree is height-balanced, and false otherwise.
Example 1
Input: root = [3,9,20,null,null,15,7]
Output: true
Every node's two subtrees differ in height by at most 1.
Example 2
Input: root = [1,2,2,3,3,null,null,4,4]
Output: false
The left subtree is much deeper than the right, so the root fails the balance condition.
Constraints
The number of nodes in the tree is in the range [0, 5000].-10^4 <= Node.val <= 10^4See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.
FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.
750+ engineers trained · frontend, backend, AWS & AI