You have been given the root of two binary trees ‘ROOT1’ and ‘ROOT2’. You need to find if the two trees are flip equivalent or not after performing some flip operations on ‘TREE1’.
A flip operation is defined as: Choose any node and swap the left and right subtrees of that node.
Note:All the values of the tree are unique.
For Example:
For the given binary trees

Tree 1 can be made flip equivalent to Tree 2 by flipping the left and right sub trees of the node with value = 2.
The first line contains an integer 'T' which denotes the number of test cases. Then the test cases are as follows.
The first and second line of each test case contains elements of the tree in the level order form. The line consists of values of nodes separated by a single space. In case a node is null, we take -1 in its place.

For example, the input for the tree depicted in the above image would be :
1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1
Explanation :
Level 1 :
The root node of the tree is 1
Level 2 :
Left child of 1 = 2
Right child of 1 = 3
Level 3 :
Left child of 2 = 4
Right child of 2 = null (-1)
Left child of 3 = 5
Right child of 3 = 6
Level 4 :
Left child of 4 = null (-1)
Right child of 4 = 7
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 6 = null (-1)
Right child of 6 = null (-1)
Level 5 :
Left child of 7 = null (-1)
Right child of 7 = null (-1)
Note:
The above format was just to provide clarity on how the input is formed for a given tree.
The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output Format:
For each test, print “Yes” if the two trees are flip equivalent. Otherwise, print “No”.
Print the output of each test case in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 50
0 <= N <= 10^3
1 <= DATA <= 10^4
Where ‘N’ is the total number of nodes in the binary tree, and “DATA” is the value of the binary tree node.
Time Limit: 1 sec
2
7 9 13 8 -1 4 2 -1 -1 -1 -1 -1 -1
7 13 9 2 4 8 -1 -1 -1 -1 -1 -1 -1
14 2 -1 5 -1 -1 -1
16 -1 2 5 -1 -1 -1
Yes
No
Test Case 1: The binary tree in the first test case will be
If we flip left and right subtrees of Tree1 having node values 7 and 13, the resulting tree will become equivalent to Tree2. Therefore given two trees are flip equivalent.
Test Case 2: As the value of the root node in both the trees are different. So they can never be flip equivalent to each other.
2
8 21 17 -1 -1 -1 -1
8 21 -1 17 -1 -1 -1
5 10 -1 15 -1 -1 -1
5 -1 10 15 -1 -1 -1
No
Yes
Test Case 1: As the left subtree and right subtree of the root node in both the trees are different. So they can never be flip equivalent to each other.
Test Case 2: If we flip right subtree of Tree1 to left subtree of root node, the resulting tree will become equivalent to Tree2. Therefore given two trees are flip equivalent.
Recursively check for every node of the tree.
Approach: A simple idea to check if the tree is flip equivalent or not is to use recursion. We will recursively check for every node.
Algorithm:
O(min( N1, N2 )), where N1 and N2 are the numbers of nodes in Tree1 and Tree2.
We are recursively checking for every node and this recursion will be stopped when we reach the end of either of the two trees. Therefore Time complexity is O(min(N1, N2)).
O(min(N1, N2), where N1 and N2 are the numbers of nodes in Tree1 and Tree2.
The maximum number of recursive calls at a time can be min (N1, N2). So space used by the stack for these recursive calls makes the space complexity O(min(N1, N2).