All problems

Problem 914

Even Odd Tree

Medium · 20 pts · Tree, Breadth-First Search

Problem

Number the levels of a binary tree from 0 at the root. The tree is Even-Odd if, on every even-indexed level, all values are odd and strictly increase from left to right, and on every odd-indexed level, all values are even and strictly decrease from left to right. Decide whether the given tree is Even-Odd. A breadth-first search that processes one level at a time checks both conditions as it goes.

Input

The first line contains N, the number of tokens. The second line contains N tokens describing the tree in level order (breadth-first, left to right), where 'null' marks a missing child, as on LeetCode.

Output

Print YES if the tree is Even-Odd, otherwise NO.

Constraints

1 <= number of nodes <= 100000; 1 <= value <= 10^6

Example input

13
1 10 4 3 null 7 9 12 8 6 null null 2

Example output

YES

Explanation

In 1 10 4 3 null 7 9 12 8 6 null null 2, level 0 is 1 (odd), level 1 is 10 4 (even, decreasing), level 2 is 3 7 9 (odd, increasing) and level 3 is 12 8 6 2 (even, decreasing). So the answer is YES.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Delete Nodes And Return Forest Next: Insufficient Nodes in Root to Leaf Paths