Loading...
Loading...
Loading...
A tree is balanced when, at every node, the heights of the left and right sides differ by at most 1. Print YES if the tree is balanced and NO if not. An empty tree is balanced.
Example 1
Input
3 9 20 null null 15 7
Output
YES
Balanced.
Example 2
Input
1 2 2 3 3 null null 4 4
Output
NO
Node 2 on the left is too deep on one side.
Try the problem first. Open one hint at a time.
Compute heights from the bottom.
Return a special value when a node breaks the rule, so you do not check twice.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor