Check Mirror in N-ary tree | Practice | GeeksforGeeks

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.

Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
   1          1
 / \        /  \
2   3      3    2 
As we can clearly see, the second tree
is mirror image of the first.

Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
   1          1
 / \        /  \
2   3      2    3 
As we can clearly see, the second tree
isn't mirror image of the first.

Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e;  and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.

Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n)

Constraints:
1 <= n,e <= 105
0
manishmate3211 week ago
JAVA SOLUTION:
 
static int checkMirrorTree(int n, int e, int[] A, int[] B) {              HashMap<Integer,Stack<Integer>> map = new HashMap<Integer,Stack<Integer>> ();       for(int i=0; i<2*e; i+=2){         if(!map.containsKey(A[i])){             Stack<Integer> stack = new Stack<Integer>();             stack.push(A[i+1]);             map.put(A[i],stack);         }         else            map.get(A[i]).push(A[i+1]);       }          for(int i=0; i<2*e; i+=2){        if(map.containsKey(B[i]) && map.get(B[i]).size() >0 ) {            if(B[i+1] != map.get(B[i]).peek()){            return 0;          }            map.get(B[i]).pop();              }            }       return 1;   }
+1
mailmohak163 weeks ago
C++ || STACK || O(N):
int checkMirrorTree(int n, int e, int A[], int B[]) {
        vector<stack<int>> data(n+1);
        for(int i=0;i<2*e;i+=2){
            data[A[i]].push(A[i+1]);
        }
        for(int i=0;i<2*e;i+=2){
            if(data[B[i]].size()&&data[B[i]].top()==B[i+1]){
                data[B[i]].pop();
            }
            else return 0;
        }
        for(auto x:data){
            if(x.size())return 0;
        }
        return 1;
    }
0
palashsarkar00073 weeks ago
from collections import dequeclass Solution:   def checkMirrorTree (self, n, e, A, B):       node_children_map = {}       for i in range(0, (2 * e), 2):           if (A[i] not in node_children_map): node_children_map[A[i]] = deque()           node_children_map[A[i]].append(A[i + 1])       for j in range(0, (2 * e), 2):           if ((B[j] not in node_children_map) or (not node_children_map[B[j]]) or (node_children_map[B[j]].pop() != B[j + 1])): return 0       return 1
+10
ritesh250331 month ago
 int checkMirrorTree(int n, int e, int A[], int B[]) {      unordered_map<int,stack<int>> mp;      for(int i=0;i<2*e;i+=2){          mp[A[i]].push(A[i+1]);      }       for(int i=0;i<2*e;i+=2){          if(mp[B[i]].top()!=B[i+1])return 0;          mp[B[i]].pop();      }      return 1;   }
0
ashutosh441 month ago
class Solution {   static int checkMirrorTree(int n, int e, int[] A, int[] B) {       HashMap<Integer,ArrayDeque<Integer>> h = new HashMap<Integer,ArrayDeque<Integer>>();              for(int i=0;i<2*e;i+=2){           if(h.containsKey(A[i])){               h.get(A[i]).push(A[i+1]);           }else{               h.put(A[i], new ArrayDeque<Integer>());               h.get(A[i]).push(A[i+1]);           }       }       for(int i=0;i<2*e;i+=2){           if(h.containsKey(B[i])){               int t = h.get(B[i]).pop();               if(t!=B[i+1]){                   return 0;               }           }else{               return 0;           }       }       return 1;   }};
0
greatwhite1 month ago
C++ solution
class Solution {
  public:
    int checkMirrorTree(int n, int e, int A[], int B[]) {
        unordered_map<int, vector<int>> a, b;
        for(int i = 0; i < 2 * e; i += 2) {
            a[A[i]].push_back(A[i + 1]);
            b[B[i]].push_back(B[i + 1]);
        }
        
        for(auto it: a) {
            auto cur = b[it.first];
            reverse(cur.begin(), cur.end());
            if(cur != it.second) return 0;
        }
        
        return 1;
    }
};
0
rohitpatil61 month ago
Solution using maps
 
map<int,vector<int>>mp1;       map<int,vector<int>>mp2;       for(int i=0;i<n+e-1;i=i+2){           mp1[A[i]].push_back(A[i+1]);       }       for(int i=0;i<n+e-1;i=i+2){           mp2[B[i]].push_back(B[i+1]);       }
       for(int i=1;i<=n;i++){           if(mp1[i].size()==mp2[i].size()){               reverse(mp2[i].begin(),mp2[i].end());               for(int j=0;j<mp2[i].size();j++){                   if(mp2[i][j]!=mp1[i][j])return 0;               }           }else{               return 0;           }       }       return 1;
0
mayankkannojia291 month ago
java .1/2.9 - hashmap approch is better and faster   static int checkMirrorTree(int n, int e, int[] A, int[] B) {       HashMap<Integer,ArrayList<Integer>> hash = new HashMap<>();       HashMap<Integer,ArrayList<Integer>> hash1 = new HashMap<>();      for(int i =0;i<A.length;i=i+2){           if(hash.containsKey(A[i])){               ArrayList<Integer> h = hash.get(A[i]);               h.add(A[i+1]);               hash.put(A[i],h);           }else {               ArrayList<Integer> h = new ArrayList<>();               h.add(A[i+1]);               hash.put(A[i],h);           }           if(hash1.containsKey(B[i])){               ArrayList<Integer> h = hash1.get(B[i]);               h.add(B[i+1]);               hash1.put(B[i],h);           }else {               ArrayList<Integer> h = new ArrayList<>();               h.add(B[i+1]);               hash1.put(B[i],h);           }       }       for(int i:hash.keySet()){           int s = hash.get(i).size();           if(hash1.containsKey(i) && s== hash1.get(i).size()) for(int k = 0;k<s;k++){ if(hash.get(i).get(k) != hash1.get(i).get(s-k-1)) return 0;}           else return 0;       } return 1;   } 
0
ashutosh1999001 month ago
JAVA Beginner Sol | O(n)

class Solution {
    static int checkMirrorTree(int n, int e, int[] arr1, int[] arr2) {
        Map<Integer,Stack<Integer>> hs=new LinkedHashMap<>();
        for(int i=1;i<2*e;i+=2){
            int k=arr1[i];
            Stack<Integer> st=hs.getOrDefault(arr1[i-1],new Stack<>());
            st.push(arr1[i]);
            hs.put(arr1[i-1],st);
        }
        for(int i=1;i<2*e;i+=2){
            int k=arr2[i];
            Stack<Integer> st=hs.getOrDefault(arr2[i-1],new Stack<>());
                int ele=-1;
                if(st.size()>0)
                ele=st.peek();
                if(ele!=arr2[i]) return 0;
                else{
                    st.pop();
                }
            hs.put(arr2[i-1],st);
        }
        return 1;
       
    }
};
0
pulkit1610011 month ago
Simplest Code Ever
Stack<Integer> st=new Stack<>();
        Queue<Integer> q=new LinkedList<>();
        int A_i=0;
        int B_i=0;
        for(int i=1;i<=n;i++){
            while(A_i<2*e && A[A_i]==i){
                st.push(A[A_i+1]);
                A_i+=2;
            }
            while(B_i<2*e && B[B_i]==i){
                q.add(B[B_i+1]);
                B_i+=2;
            }
            
            while(!q.isEmpty()){
                if(q.remove()!=st.pop()){
                    return 0;
                }
            }
        }
        return 1;
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.