Description
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.
Examples
Input:
root = [3,9,20,null,null,15,7]Output:
trueExplanation:
The tree is height-balanced.
Input:
root = [1,2,2,3,3,null,null,4,4]Output:
falseExplanation:
The tree exceeds a height difference of 1 between left and right subtrees, making it unbalanced.
Input:
root = [1,2,null,3,null,4]Output:
falseExplanation:
This is a left-skewed tree where each node only has a left child. The root node has a left subtree of height 3 and a right subtree of height 0, making the height difference 3, which violates the balance condition (difference must be ≤ 1).
Constraints
- •
Number of nodes is in range [0, 5000] - •
-10⁴ ≤ Node.val ≤ 10⁴