Check Mirror Tree
Given two n-ary trees.?Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has?2*e space separated values u,v denoting an edge from u to v for the both trees.
Approach :
int checkMirrorTree(int n, int e, int A[], int B[]) {
unordered_map<int, vector<int>>m1, m2;
for(int i=0 ; i<2*e ; i+=2){
m1[A[i]].push_back(A[i+1]);
m2[B[i]].push_back(B[i+1]);
}
for(int i=1 ; i<n+1 ; i++){
reverse(m1[i].begin(), m1[i].end());
if(m1[i] != m2[i]){
return 0;
}
}
return 1;
}
Time Complexity : O(e)
Space Complexity : O(e)
Student || 3??Codechef (Max rating -1629) || 5?? C++/Problem Solving Hackerrank ||Codeforces Max Rating -1166|| CP Enthusiast || Problem Solver & Daily Streak Gold??Codechef || 300+Codechef || 200+ Codeforces
4 个月Very helpful