A 2-tree is a tree in which each vertex either has exactly 2 non-empty
children (its left and right child),
or is a leaf (has no children). Each leaf is named with an uppercase letter,
and each other vertex is named with a lowercase letter.
We want to mutate a given 2-tree by swapping the locations of two of its subtrees.
For example, below is shown a 2-tree and then its mutation when its
subtrees rooted at C and x are swapped.
q q
x z ==> C z
A B C g x g
R Q A B R Q
Each 2-tree can be represented by a string consisting of the names of its
vertices in the order given by a post-order traversal of the tree (see notes).
Given tree, the representation of a 2-tree, and the 0-based indices of two of its vertices,
return the representation of the mutated tree. If the two subtrees have
any vertices in common return the 7-character string "OVERLAP". If tree is
not the representation of any 2-tree, return "BADTREE".
|