Two Mirror Trees | Practice | GeeksforGeeks

Given a Two Binary Trees, write a function that returns true if one is mirror of other, else returns false.
             
Example 1:
Input:
T1:     1     T2:     1
      /   \         /   \
     2     3       3     2
Output: 1

Example 2:
Input:
T1:     10      T2:   10
       /  \          /  \
      20  30        20  30
     /  \          /  \
    40   60       40  60
Output: 0

Your Task:
You don't need to take input. Just complete the function areMirror() that takes root node of two tree as parameter and returns true, if one is the mirror of other else returns false. (The driver's code print 1 if the returned value is true, otherwise 0)
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Tree).

Constraints:
1 <= Number of nodes<= 10000
-1000 <= Data of a node<= 1000

 
0
anuragkr8981 month ago
Java solution in 2 lines!
class Solution {
    boolean areMirror(Node a, Node b) {
        if(a==null || b==null) return a==b;
        return (a.data==b.data) && areMirror(a.left, b.right) && areMirror(a.right, b.left);
    }
}
0
vrajeshmodi992 months ago
int areMirror(Node* a, Node* b) {
        if(!a && !b)return 1;
        
        if(!a || !b)return 0;
        
        if(a->data != b->data)return 0;
        
        return areMirror(a->left,b->right) || areMirror(a->right,b->left);
    }
0
anonymous0022 months ago
The Simplest One !!
class Solution {
   boolean areMirror(Node a, Node b) {
       
       if(a == null || b == null){
           return a == b ; 
       }
       
       if(a.data != b.data){
           return false ;
       }
       
       return areMirror(a.left , b.right) &&  areMirror(a.right,b.left) ;
       
   }
}
0
mohitm15092 months ago
//Java Time(0.5)
class Solution {   boolean areMirror(Node a, Node b) {       // Your code here       if(a== null && b == null) return true;       if(a != null && b== null  || a == null && b != null) return false;       if ((a!=null && b!=null) && (a.data == b.data))           return(areMirror(a.left,b.right) && areMirror(a.right,b.left));       return false;   }}
+1
vedirajnish2 months ago
class Solution {
    boolean areMirror(Node a, Node b) {
        if(a==null && b==null)return true;
        if(a==null &&b!=null ||a!=null &&  b==null)return false;
        if(a.data!=b.data)return false;
        return areMirror(a.left,b.right);
    }
}
0
abhisheksanwal2 months ago
Simple Python Solution:
 
def areMirror(self,root1,root2):       '''       :param root1,root2: two root of the given trees.       :return: True False              '''       if root1 is None and root2 is None:           return True                  elif root1 is not None and root2 is not None:           return root1.data==root2.data and self.areMirror(root1.left,root2.right) and self.areMirror(root1.right,root2.left)              else:           return False
0
hridayec202 months ago
int areMirror(Node* a, Node* b)    {   // Y    if(a ==NULL && b==NULL)       return 1;    if(a ==NULL && b!=NULL || a !=NULL && b==NULL)       return 0;    if(a->data!=b->data)       return 0;    if(a->data==b->data)    {        areMirror(a->left,b->right);    }
+1
ashishbutola73 months ago
Javascript -:   
 areMirror(root1,root2){   //code here      const mirror = (root) => {       if (!root) return null       let leftNode = mirror(root.right)       let rightNode = mirror(root.left)       root.left = leftNode       root.right = rightNode       return root   }      mirror (root1)      if (JSON.stringify(root1) === JSON.stringify(root2)) return true   return false  }
0
mayank20213 months ago
C++class Solution{   public:   int areMirror(Node* a, Node* b) {
   if(!a && !b)    return 1;   else if (a==NULL || b==NULL)    return 0;   else if(a->data != b->data)     return 0;   else if ( (areMirror(a->left, b->right)) && (areMirror(a->right, b->left))   )     return 1;
   }
};
+1
kronizerdeltac3 months ago
JAVA RECURSIVE SOLUTION
 
NOTE: Similar questions :
https://practice.geeksforgeeks.org/problems/determine-if-two-trees-are-identical/1https://practice.geeksforgeeks.org/problems/symmetric-tree/1
https://practice.geeksforgeeks.org/problems/determine-if-two-trees-are-identical/1
https://practice.geeksforgeeks.org/problems/symmetric-tree/1
boolean areMirror(Node root1, Node root2) {       if (root1 == null && root2 == null)           return true;
       if (root1 != null && root2 != null && root1.data == root2.data && areMirror(root1.left, root2.right) && areMirror(root1.right, root2.left))           return true;       else           return false;   }
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.