A tree is height-balanced if, for every node, the heights of its left and right subtrees differ by at most 1. Return True if the tree is balanced.
The tests write a tree as a level-order list (row by row, left to right) with None for a missing child: [3, 9, 20, None, None, 15, 7] is a root 3 with children 9 and 20, where 20 has children 15 and 7.
For [3, 9, 20, None, None, 15, 7] the answer is True; for [1, 2, 2, 3, 3, None, None, 4, 4] it is False.
Example 1
Input:
is_balanced(root) # root = [3, 9, 20, None, None, 15, 7]
Output:
True
Example 2
Input:
is_balanced(root) # root = [1, 2, 2, 3, 3, None, None, 4, 4]
Output:
False
0 <= number of nodes <= 100000No results yet
Run your code to check it against the examples, or submit to run every test.