In the book Introduction To Algorithms - A Creative Approach, Question 4.24:
Let T1, and T2 be two arbitrary trees, each having n nodes. Prove that it is sufficient to apply at most 2n rotations to T1, so that it becomes equal to T2.
For binary search trees, I figured out an algorithm:
Find the element which is equal to T2's root, let's call it target-root.
Using AVL rotation strategy, rotate target-root to make it T1's new root.
During this process, multiple rotations may be performed.For the left sub tree of T1 and T2, process them as above recursively.
For the right sub tree of T1 and T2, process them as above recursively.
This algorithm, when in worst-case, runs in O(N^2).
I don't quite understand the phrase “arbitrary trees”, and I can't figure out how to make T1 equal to T2.
Could anyone help on this question?