#K72792. Mirror Tree Check
Mirror Tree Check
Mirror Tree Check
Given two rooted trees represented in a special serialized format, determine whether they are mirror images of each other.
A tree is serialized as follows: a node is represented by its integer value followed optionally by a list of children enclosed in parentheses. The children are separated by commas. For example, a tree with root 1 having two children 2 and 3 is represented as 1(2,3)
. If a node has no children, it is represented simply by its value. The special string null
denotes an empty tree.
Two trees are considered mirror images if their root values are equal, they have the same number of children, and for each index i, the i-th child of the first tree is a mirror image of the (n-i-1)-th child of the second tree (where n is the number of children).
inputFormat
The input consists of two lines. The first line is the serialized representation of the first tree, and the second line is the serialized representation of the second tree. If a tree is empty, the line will contain null
.
Note: There are no spaces in the input.
outputFormat
Output a single line: True
if the two trees are mirror images of each other, and False
otherwise.
1(2,3)
1(3,2)
True