Check Balanced Binary Tree
Mediumjavapythonccppjavascript
Determine whether a binary tree is height-balanced: for every node, the heights of its left and right subtrees differ by at most 1.
Input Format
The first line contains an integer M, the number of level-order tokens. The second line contains M space-separated tokens (integers or null). It is blank when M is 0.
Output Format
Print 1 if the tree is balanced, otherwise print 0.
Example 1
Input
7 3 9 20 null null 15 7
Output
1
Explanation: Every node's subtree heights differ by at most 1.
- 0 <= number of nodes <= 5000
- -1000000000 <= node value <= 1000000000