Balanced Binary Tree
Part of Practice: Recursive Foundations
Given the root of a binary tree, decide whether it is height-balanced: at every node, the heights of the left and right subtrees differ by at most 1. Return true or false.
The tree can be empty (an empty tree is balanced) and has at most 5,000 nodes. Note the condition must hold at every node, not just the root.
Examples
- Input:
- root = [3,9,20,null,null,15,7]
- Output:
- true
At every node, the two subtree heights differ by at most 1.
- Input:
- root = [1,2,2,3,3,null,null,4,4]
- Output:
- false
At the node 2 on the left, the left subtree has height 2 and the right has height 0 — a difference of 2.
- Input:
- root = []
- Output:
- true
Hints
One at a time — struggle first, peek second.
Your thinking space — verify on LeetCode, then mark it.