Determine if Two Trees are Identical | Practice | GeeksforGeeks

Given two binary trees, the task is to find if both of them are identical or not.  

Example 2:
Input:
     1          1
   /   \      /   \
  2     3    2     3
Output: Yes
Explanation: There are two trees both
having 3 nodes and 2 edges, both trees
are identical having the root as 1,
left child of 1 is 2 and right child
of 1 is 3.
Example 2:
Input:
    1       1
  /  \     /  \
 2    3   3    2
Output: No
Explanation: There are two trees both
having 3 nodes and 2 edges, but both
trees are not identical.

Your task:
Since this is a functional problem you don't have to worry about input, you just have to complete the function isIdentical() that takes two roots as parameters and returns true or false. The printing is done by the driver code.

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Tree).

Constraints:
1 <= Number of nodes <= 105
1 <=Data of a node <= 105
+1
sc23333 days ago
Easy to Understand Solution  if(r1==NULL && r2==NULL)       return true;       if(r1==NULL || r2==NULL)       return false;       if(r1->data!=r2->data)       return false;              return isIdentical(r1->left,r2->left)&& isIdentical(r1->right,r2->right);
0
adarshsharadpandey235 days ago
if(r1==NULL || r2==NULL)
        {
            if(r1==NULL && r2==NULL)
                return 1;
            return 0;
        }
        else
        {
            if(r1->data==r2->data)
            {
                return (isIdentical(r1->left,r2->left)&&isIdentical(r1->right,r2->right));
            }
            else
            {
                return 0;
            }
        }
0
born2code6 days ago
boolean isIdentical(Node root1, Node root2)
	{
	    if(root1==null&&root2==null)
	        return true;
	   if((root1==null&&root2!=null)||(root1!=null&&root2==null))
	        return false;
	   if(root1.data==root2.data)
	        return isIdentical(root1.left,root2.left)&&isIdentical(root1.right,root2.right);
        else
            return false;  
	}
0
itachinamikaze2211 week ago
 boolean isIdentical(Node root1, Node root2){    if(root1==null||root2==null)        return (root1==root2); boolean var1=  isIdentical(root1.left, root2.left); boolean var2=  isIdentical(root1.right, root2.right); return(root1.data==root2.data)&& var1&& var2;}
+3
brijesh202232 weeks ago
C++ Iterative Solution: 
    bool isIdentical(Node *r1, Node *r2)   {       //Your Code here       stack<pair<Node*,Node*>> st;       if(!r1 && !r2) return true;       else if(!r1 || !r2) return false;       else{           st.push({r1,r2});                      while(!st.empty()){               r1 = st.top().first;               r2 = st.top().second;               st.pop();               if(r1->data != r2->data) return false;               if(r1->left && r2->left) st.push({r1->left , r2->left});               else if (r1->left || r2->left) return false;               if(r1->right && r2->right) st.push({r1->right , r2->right});               else if (r1->right || r2->right) return false;           }           return true;       }     }
0
deveshkumar12 weeks ago
 bool isIdentical(Node *r1, Node *r2)   {       //Your Code here       if(r1==NULL && r2==NULL) return true;       else if(r1==NULL || r2==NULL) return false;       if(r1->data==r2->data)       {           isIdentical(r1->left,r2->left);           isIdentical(r1->right,r2->right);       }       else return false;   }
0
singhdipranjan672 weeks ago
boolean isIdentical(Node root1, Node root2)
	{
	if(root1==null && root2==null) return true;
    if((root1==null && root2!=null) || (root1!=null && root2==null)) return false;
    return (root1.data==root2.data && isIdentical(root1.left,root2.left) && isIdentical(root1.right,root2.right));
	}
0
ghostlearner213 weeks ago
JAVA Solution all test cases passed !  
boolean isIdentical(Node root1, Node root2){    boolean var = false;        if(root1==null && root2==null){        return true;    }    if(root1==null || root2==null){        return false;    }    boolean var1 = isIdentical(root1.left, root2.left); //check left subtress    boolean var2 = isIdentical(root1.right, root2.right); //check right subtrees    if(root1.data == root2.data && var1 && var2){ 
    // check if values are equal along with identical left and right subtrees       var = true;    }else{        var =  false;    }        return var;}
0
rajesh behera3 weeks ago
JAVA SOLUTION
boolean isIdentical(Node root1, Node root2){    // Code Here    if(root1==null && root2==null) return true;    if(root1==null || root2==null) return false;    boolean lr=isIdentical(root1.left,root2.left);    boolean rr=isIdentical(root1.right,root2.right);    return (root1.data==root2.data && lr && rr);}
0
nitishkumarkushwaha18133 weeks ago
time 0.6 
 
class Solution{   //Function to check if two trees are identical.   static void Inorder(Node root , ArrayList<Integer> al){       if(root != null){           al.add(root.data);           Inorder(root.left , al);           Inorder(root.right , al);                  }   }boolean isIdentical(Node root1, Node root2){    // Code Here    ArrayList<Integer> l1 = new ArrayList<Integer>();    ArrayList<Integer> l2 = new ArrayList<Integer>();        Inorder(root1 , l1);    Inorder(root2 , l2);        if(l1.equals(l2))        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.