#K91612. Symmetric Binary Tree

    ID: 38015 Type: Default 1000ms 256MiB

Symmetric Binary Tree

Symmetric Binary Tree

You are given a binary tree represented as a list of node descriptions. Each node is described by a triplet (val, left, right), where val is the integer value of the node, and left and right are the indices of its left and right children respectively. A value of -1 indicates that the corresponding child does not exist. The first node (index 0) is the root of the tree.

A binary tree is symmetric if it is a mirror reflection of itself. Formally, a tree is symmetric if for every node, the left subtree is the mirror image of the right subtree. This can be expressed as:

$$T_L(v) = \text{mirror}(T_R(v))$$

Your task is to determine whether the given binary tree is symmetric. Print YES if the tree is symmetric, and NO otherwise.

inputFormat

The first line contains an integer N representing the number of nodes in the tree.
Each of the following N lines contains three integers: val, left, and right. The left and right values represent the indices of the left and right children respectively, or -1 if there is no such child.

For example:

3
1 1 2
2 -1 -1
2 -1 -1

outputFormat

Output a single line: YES if the tree is symmetric, and NO otherwise.

For the above sample input, the output should be:

YES
## sample
3
1 1 2
2 -1 -1
2 -1 -1
YES