All problems

Problem 148

Symmetric Tree

Easy · 10 pts · Tree, Breadth-First Search, Binary Tree

Problem

You are given a binary tree. Decide whether it is symmetric, that is, whether it is a mirror image of itself around a vertical line through the root. Two subtrees are mirrors of each other when their roots have equal values, the left subtree of one mirrors the right subtree of the other, and the right subtree of one mirrors the left subtree of the other. The whole tree is symmetric when the root's left and right subtrees are mirrors.

Input

First line: integer N, the number of tokens. Second line: N tokens describing the tree in level order, where 'null' marks a missing child (the same format LeetCode uses).

Output

Print YES or NO.

Constraints

1 <= number of nodes <= 1000; -100 <= value <= 100

Example input

7
1 2 2 3 4 4 3

Example output

YES

Explanation

The root 1 has two children 2. The left 2 has children 3 and 4, and the right 2 has children 4 and 3. Read from the outside in, the values match (3 with 3, 4 with 4), so the tree is symmetric: YES.

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

Previous: Same Tree Next: Diameter of Binary Tree