Олимпиадный тренинг

Задача 44649. balance


Задача

Темы: Деревья
A tree is said to be balanced if, for any of its vertices, the heights of the left and right subtrees for that vertex differ by no more than 1.

Input
A sequence of integers ending in zero is entered. Zero itself is not included in the sequence. Build a tree corresponding to the given sequence.

Output
Determine if the tree is balanced, output the word YES or NO.
 
Examples
# Input Output
1
7 3 2 1 9 5 4 6 8 0
YES