


For the given example below, the trees are mirror images of each other.
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains a single integer, the ‘N’ denoting the number of vertices in both the trees.
Next, ‘N’-1 lines have two integers i,j, denoting an edge between vertex i and vertex j in ‘TREE_A’.
Next, ‘N’-1 lines have two integers i,j, denoting an edge between vertex i and vertex j in ‘TREE_B’.
For each test case, print ‘YES’ if both the trees are mirror images of each other.
Print the output of each test case in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 1000.
0 <= Node Label <=N-1.
Time limit: 1 sec
We can simply observe the pattern that if the trees are mirror images of each other, then the i’th node of TREE_A and i’th node of TREE_B should have an equal number of child nodes and the same set of child nodes but in reverse order as they should form the mirror images.
In this approach, we will simply check the ‘TREE_A[i]’ and ‘TREE_B[i]’ for all labels from 0 to ‘N’-1 and check whether they consist of the same set of child nodes in reverse order or not. If we find any discrepancy, we will return ‘FALSE’.Else the answer will be ‘TRUE’.
In this approach, we will first traverse the first tree in a preorder manner and store the nodes in a stack, and for the second tree, we will traverse the tree in a postorder manner and store the nodes in a queue. We are using stack and queue because we want to check the reverse order, and we know that Stack follows FIFO(First In First Out) and Queue follows FILO(First In Last Out). After the traversals, we will pop values one by one from the stack and queue and compare them. If we find any discrepancy, we will return ‘FALSE’.Otherwise, the trees are mirror images, we will return ‘TRUE’.