Initialize a set say St to store all the unique nodes' values.
Traverse the arrayA[] using the variable i, and performing the following steps:
Push the value A[i][1] into the vector T1[A[i][0]] and then append the A[i][0] and A[i][1] to the set St.
As the edges are given according to the level order traversal, therefore in tree A for all nodes, the left child was inserted first and then the right child was inserted.
Traverse the array B[] in reverse order using the variable i and performing the following steps:
Push the value B[i][1] into the vector T2[B[i][0]] and then append the B[i][0] and B[i][1] to the set St.
As array B[] is traversed in reverse, therefore in the tree B for all nodes, the right child was inserted first and then the left child was inserted.
Now iterate over the of setSt and check if the vectors of children of the current node in tree A are not equal to the vectors of children of the current node in tree B, then print "No" as the answer and then return.
Finally, if none of the above cases satisfy, then print "Yes" as the answer.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include<bits/stdc++.h>usingnamespacestd;// Function to check whether two binary// trees are mirror image of each other// or notstringcheckMirrorTree(intN,intM,intA[][2],intB[][2]){// Stores the adjacency list// of tree Amap<int,vector<int>>T1;// Stores the adjacency list// of tree Bmap<int,vector<int>>T2;// Stores all distinct nodesset<int>st;// Traverse the array A[]for(inti=0;i<M;i++){// Push A[i][1] in the// vector T1[A[i][0]]T1[A[i][0]].push_back(A[i][1]);// Insert A[i][0] in the// set stst.insert(A[i][0]);// Insert A[i][1] in the// set stst.insert(A[i][1]);}// Traverse the array B[] in// reversefor(inti=M-1;i>=0;i--){// Push B[i][1] in the// vector T2[B[i][0]]T2[B[i][0]].push_back(B[i][1]);// Insert B[i][0] in the// set stst.insert(B[i][0]);// Insert B[i][0] in the// set stst.insert(B[i][1]);}// Iterate over the set stfor(autonode:st){// If vector T1[node] is// not equals to T2[node]if(T1[node]!=T2[node])return"No";}// Return "Yes" as// the answerreturn"Yes";}// Driver Codeintmain(){// Given InputintN=6;intM=5;intA[][2]={{1,5},{1,4},{5,7},{5,8},{4,9}};intB[][2]={{1,4},{1,5},{4,9},{5,8},{5,7}};// Function Callcout<<checkMirrorTree(N,M,A,B);return0;}
Java
// Java program for the above approachimportjava.util.*;publicclassMain{// Function to check whether two binary// trees are mirror image of each other// or notstaticStringcheckMirrorTree(intN,intM,int[][]A,int[][]B){// Stores the adjacency list// of tree AHashMap<Integer,Vector<Integer>>T1=newHashMap<Integer,Vector<Integer>>();// Stores the adjacency list// of tree BHashMap<Integer,Vector<Integer>>T2=newHashMap<Integer,Vector<Integer>>();// Stores all distinct nodesSet<Integer>st=newHashSet<Integer>();// Traverse the array A[]for(inti=0;i<M;i++){// Push A[i][1] in the// vector T1[A[i][0]]if(T1.containsKey(A[i][0])){T1.get(A[i][0]).add(A[i][1]);}else{T1.put(A[i][0],newVector<Integer>());T1.get(A[i][0]).add(A[i][1]);}// Insert A[i][0] in the// set stst.add(A[i][0]);// Insert A[i][1] in the// set stst.add(A[i][1]);}// Traverse the array B[] in// reversefor(inti=M-1;i>=0;i--){// Push B[i][1] in the// vector T2[B[i][0]]if(T2.containsKey(B[i][0])){T2.get(B[i][0]).add(B[i][1]);}else{T2.put(B[i][0],newVector<Integer>());T2.get(B[i][0]).add(B[i][1]);}// Insert B[i][0] in the// set stst.add(B[i][0]);// Insert B[i][0] in the// set stst.add(B[i][1]);}// Iterate over the set stfor(intnode:st){// If vector T1[node] is// not equals to T2[node]if(!(T1.get(node)==T2.get(node)))return"Yes";}// Return "No" as// the answerreturn"No";}publicstaticvoidmain(String[]args){// Given InputintN=6;intM=5;int[][]A={{1,5},{1,4},{5,7},{5,8},{4,9}};int[][]B={{1,4},{1,5},{4,9},{5,8},{5,7}};// Function CallSystem.out.print(checkMirrorTree(N,M,A,B));}}// This code is contributed by rameshtravel07.
Python3
# Py program for the above approach# Function to check whether two binary# trees are mirror image of each other# or notdefcheckMirrorTree(N,M,A,B):# Stores the adjacency list# of tree AT1=[[]foriinrange(100)]# Stores the adjacency list# of tree BT2=[[]foriinrange(100)]# Stores all distinct nodesst={}# Traverse the array A[]foriinrange(M):# Push A[i][1] in the# vector T1[A[i][0]]T1[A[i][0]].append(A[i][1])# Insert A[i][0] in the# set stst[A[i][0]]=1# Insert A[i][1] in the# set stst[A[i][1]]=1# Traverse the array B[] in# reverseforiinrange(M-1,-1,-1):# Push B[i][1] in the# vector T2[B[i][0]]T2[B[i][0]].append(B[i][1])# Insert B[i][0] in the# set stst[B[i][0]]=1# Insert B[i][0] in the# set stst[B[i][1]]=1# Iterate over the set stfornodeinst:# If vector T1[node] is# not equals to T2[node]if(T1[node]!=T2[node]):return"No"# Return "Yes" as# the answerreturn"Yes"# Driver Codeif__name__=='__main__':# Given InputN=6M=5A=[[1,5],[1,4],[5,7],[5,8],[4,9]]B=[[1,4],[1,5],[4,9],[5,8],[5,7]]# Function Callprint(checkMirrorTree(N,M,A,B))# This code is contributed by mohit kumar 29.
C#
// C# program for the above approachusingSystem;usingSystem.Collections.Generic;classGFG{// Function to check whether two binary// trees are mirror image of each other// or notstaticstringcheckMirrorTree(intN,intM,int[,]A,int[,]B){// Stores the adjacency list// of tree ADictionary<int,List<int>>T1=newDictionary<int,List<int>>();// Stores the adjacency list// of tree BDictionary<int,List<int>>T2=newDictionary<int,List<int>>();// Stores all distinct nodesHashSet<int>st=newHashSet<int>();// Traverse the array A[]for(inti=0;i<M;i++){// Push A[i][1] in the// vector T1[A[i][0]]if(T1.ContainsKey(A[i,0])){T1[A[i,0]].Add(A[i,1]);}else{T1[A[i,0]]=newList<int>();T1[A[i,0]].Add(A[i,1]);}// Insert A[i][0] in the// set stst.Add(A[i,0]);// Insert A[i][1] in the// set stst.Add(A[i,1]);}// Traverse the array B[] in// reversefor(inti=M-1;i>=0;i--){// Push B[i][1] in the// vector T2[B[i][0]]if(T2.ContainsKey(B[i,0])){T2[B[i,0]].Add(B[i,1]);}else{T2[B[i,0]]=newList<int>();T2[B[i,0]].Add(B[i,1]);}// Insert B[i][0] in the// set stst.Add(B[i,0]);// Insert B[i][0] in the// set stst.Add(B[i,1]);}// Iterate over the set stforeach(intnodeinst){// If vector T1[node] is// not equals to T2[node]if(!T1[node].Equals(T2[node]))return"Yes";}// Return "No" as// the answerreturn"No";}staticvoidMain(){// Given InputintN=6;intM=5;int[,]A={{1,5},{1,4},{5,7},{5,8},{4,9}};int[,]B={{1,4},{1,5},{4,9},{5,8},{5,7}};// Function CallConsole.Write(checkMirrorTree(N,M,A,B));}}// This code is contributed by divyesh072019.
JavaScript
<script>// Javascript program for the above approach// Function to check whether two binary// trees are mirror image of each other// or notfunctioncheckMirrorTree(N,M,A,B){// Stores the adjacency list// of tree AletT1=[];// Stores the adjacency list// of tree BletT2=[];for(leti=0;i<100;i++){T1.push([]);T2.push([]);}// Stores all distinct nodesletst=newMap();// Traverse the array A[]for(leti=0;i<M;i++){// Push A[i][1] in the// vector T1[A[i][0]]T1[A[i][0]].push(A[i][1]);// Insert A[i][0] in the// set stst[A[i][0]]=1;// Insert A[i][1] in the// set stst[A[i][1]]=1;}// Traverse the array B[] in// reversefor(leti=M-1;i<-1;i=-1){// Push B[i][1] in the// vector T2[B[i][0]]T2[B[i][0]].push(B[i][1]);// Insert B[i][0] in the// set stst[B[i][0]]=1;// Insert B[i][0] in the// set stst[B[i][1]]=1;}// Iterate over the set stst.forEach((values,node)=>{// If vector T1[node] is// not equals to T2[node]if(T1[node]!=T2[node]){return"No";}})// Return "Yes" as// the answerreturn"Yes";}// Given InputletN=6;letM=5;letA=[[1,5],[1,4],[5,7],[5,8],[4,9]];letB=[[1,4],[1,5],[4,9],[5,8],[5,7]];// Function Calldocument.write(checkMirrorTree(N,M,A,B));// This code is contributed by divyeshrabadiya07.</script>
Output
Yes
Time Complexity: O((N+M)*log(N)) Auxiliary Space: O(N+M)