385. Height-Balanced Tree
EasyDFS
Print YES if, for every node, the heights of its two subtrees differ by at most one, else NO.
Input format
Line 1: n (number of level-order entries) Line 2: the values in level order, -1 for a missing child
Output format
YES or NO
Example 1
Input
7 3 9 20 -1 -1 15 7
Output
YES
Example 2
Input
9 1 2 2 3 3 -1 -1 4 4
Output
NO
Constraints
1 ≤ nodes ≤ 5000
Read input from standard input and print the answer to standard output. Your code is judged against 5 test cases, most of them hidden.
Loading the MyLoginn IDE…