Check if Tree is Isomorphic | Practice | GeeksforGeeks

Given two Binary Trees. Check whether they are Isomorphic or not.
Note: 
Two trees are called isomorphic if one can be obtained from another by a series of flips, i.e. by swapping left and right children of several nodes. Any number of nodes at any level can have their children swapped. Two empty trees are isomorphic.
For example, the following two trees are isomorphic with the following sub-trees flipped: 2 and 3, NULL and 6, 7 and 8.

Example 1:
Input:
 T1    1     T2:   1
     /   \        /  \
    2     3      3    2
   /            /
  4            4
Output: No


Example 2:
Input:
T1    1     T2:    1
    /  \         /   \
   2    3       3     2
  /                    \
  4                     4
Output: Yes

Your Task:
You don't need to read input or print anything. Your task is to complete the function isomorphic() that takes the root nodes of both the Binary Trees as its input and returns True if the two trees are isomorphic. Else, it returns False. (The driver code will print Yes if the returned values are true, otherwise false.)
 
Expected Time Complexity: O(min(M, N)) where M and N are the sizes of the two trees.
Expected Auxiliary Space: O(min(H1, H2)) where H1 and H2 are the heights of the two trees.
Constraints:
1<=Number of nodes<=105
0
ishajaiswal445563 days ago
 
    bool isIsomorphic(Node *root1,Node *root2)
   {
       if(root1==NULL && root2==NULL)
       {
           return true;
       }
       if(root1==NULL || root2==NULL)
       {
           return false;
       }
       bool check1=isIsomorphic(root1->left,root2->right) && isIsomorphic(root1->right , root2->left);
       bool check2=isIsomorphic(root1->left,root2->left) && isIsomorphic(root1->right , root2->right);
       bool check3 = (root1->data==root2->data);
       return (check1 || check2) && check3;
   //add code here.
   }
+1
tanashah1 week ago
       if(root1==NULL && root2==NULL) return true;
       if(root1==NULL || root2==NULL) return false;
       if(root1->data!=root2->data) return false;
       //mirror tree ke liye
       int a=isIsomorphic(root1->left,root2->right) && isIsomorphic(root1->right,root2->left);
       //abhi same tree keliey 
       int b=isIsomorphic(root1->left,root2->left) && isIsomorphic(root1->right,root2->right);
       return a||b;
0
amarrajsmart1972 weeks ago
C++ Solution.
 bool isIsomorphic(Node *root1,Node *root2)   {   //add code here.   bool a,b;   if(!root1&&!root2)   {       return true;   }   else if(!root1||!root2)   {       return false;   }   else   {    if(root1->data!=root2->data)    {       return false;     }    a=isIsomorphic(root1->left,root2->left)&&isIsomorphic(root1->right,root2->right);    b=isIsomorphic(root1->left,root2->right)&&isIsomorphic(root1->right,root2->left);    return a||b;   }      }
0
1ashishchauhan20022 weeks ago
 if(root1==NULL && root2==NULL) return true;       if(root1==NULL || root2==NULL) return false;       if(root1->data!=root2->data) return false;       int a=isIsomorphic(root1->left,root2->right) &&isIsomorphic(root1->right, root2->left);       int b=isIsomorphic(root1->left, root2->left)&&isIsomorphic(root1->right,root2->right);       return a||b;
 
0
harrypotter01 month ago
Why TLE in Python?
 

    def isIsomorphic(self, root1, root2): 
       if not root1 and not root2:
           return True
       if not root1 or not root2:
           return False
       if root1.data!= root2.data:
           return False

       return (self.isIsomorphic(root1.left, root2.left) \
       and self.isIsomorphic(root1.right, root2.right)) \
       or (self.isIsomorphic(root1.right, root2.left) \
       and self.isIsomorphic(root1.left, root2.right))

+1
gdh14031 month ago
//simple c++ solution
//if you find it helpfull then upvote it.
bool isIsomorphic(Node *root1,Node *root2)
    {
        if(!root1 || !root2)
        {//if both root null then it is valid case
            if(!root1 && !root2) return true;
            return false;
        }
        //both root value must be same for valid isomorphic
        if(root1->data != root2->data) return false;
 		/*checking weather left and right part is isomorphic or not incase if not isomorphic then we will check by swapping its left and right child  to check is isomorphic or not*/     
        bool f=(isIsomorphic(root1->left,root2->left)&&isIsomorphic(root1->right,root2->right));     
        if(!f)
        {
            return isIsomorphic(root1->left,root2->right);
        }
        return true;
        
    }
    
+1
puranjanprithu1 month ago
My python solution gives TLE ,while my C++ solution with same logic worked: WHY?????
 
# My python answer: Why is it giving TLE
def isIsomorphic(self, root1, root2): 
        #code here.
        if root1 is None and root2 is None: return True
        if root1 is None or root2 is None : return False
        if root1.data!=root2.data: return False
        l=self.isIsomorphic(root1.left,root2.left) and self.isIsomorphic(root1.right,root2.right)
        r=self.isIsomorphic(root1.left,root2.right) and self.isIsomorphic(root1.right,root2.left)
        return l or r
While my C++ solution with same logic worked:
class Solution{
  public:
    // Return True if the given trees are isomotphic. Else return False.
    bool isIsomorphic(Node *root1,Node *root2)
    {
    //add code here.
    if (!root1 and !root2) return true;
    if (!root1 or !root2) return false;
    if (root1->data!=root2->data) return false;
    bool l= isIsomorphic(root1->left,root2->left) and isIsomorphic(root1->right,root2->right);
    bool r= isIsomorphic(root1->left,root2->right) and isIsomorphic(root1->right,root2->left);
    
    return l or r;
    }
};
+2
badgujarsachin831 month ago
 bool isIsomorphic(Node *root1,Node *root2)
    {
    //add code here.
    if(root1==NULL && root2==NULL){
        return true;
    }
    if(root1==NULL || root2==NULL || root1->data!=root2->data){
        return false;
    }
    return (isIsomorphic(root1->left,root2->left) && isIsomorphic(root1->right,root2->right)) || (isIsomorphic(root1->left,root2->right) && isIsomorphic(root1->right,root2->left));
    }
+1
sanketbhagat2 months ago
SIMPLE JAVA SOLUTION
class Solution  { 
    boolean isIsomorphic(Node root1, Node root2)  { 
         // code here.
         if(root1==null && root2==null) return true;
         if(root1==null || root2==null || root1.data!=root2.data) return false;
         return (isIsomorphic(root1.left,root2.left)&&isIsomorphic(root1.right,root2.right)) || (isIsomorphic(root1.left,root2.right)&&isIsomorphic(root1.right,root2.left));
    }
} 
0
hamidnourashraf2 months ago
    def rec_isomorph(self, root1, root2):
        if root1 is None and root2 is None:
            return True
        elif root1 is None or root2 is None:
            return False
        elif root1.data != root2.data:
            return False
        else: 
            return self.rec_isomorph(root1.left, root2.left) or self.rec_isomorph(root1.left, root2.right)
    def isIsomorphic(self, root1, root2): 
        return self.rec_isomorph(root1, root2)

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.