Loading problem...
Given the root node of a binary tree, determine whether the tree satisfies the height-balanced property.
A binary tree is considered height-balanced if, for every node in the tree, the absolute difference between the heights of its left and right subtrees is at most one. This property must hold for all nodes, not just the root.
Height Definition: The height of a node is the number of edges on the longest path from that node to any leaf node. A leaf node has height 0, and an empty tree (null) has height -1.
Key Insight: A tree being height-balanced at the root does not guarantee it's balanced throughout—every subtree must independently satisfy the balance condition. This recursive property makes the problem elegant yet requires careful traversal of the entire structure.
root = [3,9,20,null,null,15,7]trueThe tree structure is:
3
/
9 20
/
15 7
At node 3: left subtree height = 1, right subtree height = 2, difference = 1 ≤ 1 ✓ At node 9: both subtrees are empty, difference = 0 ≤ 1 ✓ At node 20: left subtree height = 1, right subtree height = 1, difference = 0 ≤ 1 ✓ At nodes 15 and 7: both are leaves with no subtrees, difference = 0 ✓ Since all nodes satisfy the balance condition, the tree is height-balanced.
root = [1,2,2,3,3,null,null,4,4]falseThe tree structure is: 1 / 2 2 / 3 3 / 4 4
At node 1: left subtree height = 3, right subtree height = 1, difference = 2 > 1 ✗ The left subtree extends much deeper than the right, violating the balance condition at the root. Therefore, the tree is NOT height-balanced.
root = []trueAn empty tree (null root) is trivially considered height-balanced since there are no nodes that could violate the balance condition.
Constraints