Balanced tree example
Input
TreeNode(3, TreeNode(9), TreeNode(20, TreeNode(15), TreeNode(7)))
Output
True
Left subtree height = 1, right subtree height = 2 at root, difference is 1; all nodes satisfy balance condition.
Full lesson preview
Determine whether a binary tree is height-balanced: for every node, the heights of left and right subtrees differ by at most 1.
Problem statement
Task
Examples
Input
TreeNode(3, TreeNode(9), TreeNode(20, TreeNode(15), TreeNode(7)))
Output
True
Left subtree height = 1, right subtree height = 2 at root, difference is 1; all nodes satisfy balance condition.
Input format
Output format
Constraints
Samples
Input
TreeNode(1, TreeNode(2, TreeNode(3)))
Output
False
This is a left-skewed tree; some nodes have subtree height difference > 1.