#K70932. Tree Reordering Isomorphism

    ID: 33418 Type: Default 1000ms 256MiB

Tree Reordering Isomorphism

Tree Reordering Isomorphism

Given two binary trees, determine whether the second tree can be transformed into the first tree by swapping the left and right children of any nodes. Two trees are considered isomorphic if their structures can be made identical via a series of swaps.

Formally, if a node has left subtree \(L\) and right subtree \(R\), then two trees rooted at \(n_1\) and \(n_2\) are isomorphic if and only if:

\[ (n_1 = n_2) \quad \text{and} \quad \big(\text{isomorphic}(L(n_1),L(n_2)) \;\wedge\; \text{isomorphic}(R(n_1),R(n_2))\big) \quad \text{or} \quad (n_1 = n_2) \quad \text{and} \quad \big(\text{isomorphic}(L(n_1),R(n_2)) \;\wedge\; \text{isomorphic}(R(n_1),L(n_2))\big) \]

All node labels are unique. An empty tree (represented by 0 nodes) is isomorphic only to another empty tree.

inputFormat

The input consists of two parts describing two trees.

For each tree:

  • The first line contains an integer \(N\), the number of nodes in the tree.
  • The next \(N\) lines each contain three strings: Node, Left, and Right. If a child does not exist, it is represented by the string null.

The first node provided for each tree is considered the root.

outputFormat

Output a single line containing True if the second tree can be transformed into the first by swapping subtrees, or False otherwise.

## sample
3
A B C
B null null
C null null
3
A C B
C null null
B null null
True

</p>