Symmetric Tree | Practice | GeeksforGeeks

Given a Binary Tree. Check whether it is Symmetric or not, i.e. whether the binary tree is a Mirror image of itself or not.
Example 1:
Input:
         5
       /   \
      1     1
     /       \
    2         2
Output: True
Explanation: Tree is mirror image of
itself i.e. tree is symmetric

Example 2:
Input:
         5
       /   \
      10     10
     /  \     \
    20  20     30
Output: False
Your Task:
You don't need to read input or print anything. Your task is to complete the function isSymmetric() which takes the root of the Binary Tree as its input and returns True if the given Binary Tree is a same as the Mirror image of itself. Else, it returns False.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Tree).
Constraints:
0<=Number of nodes<=100
Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
 
0
nevilvas2 days ago
    def isSymmetric(self, root):
        # Your Code Here
        if root == None:
            return True
        return (self.solve(root.left, root.right))
    
    def solve(self,left,right):
        if left == None and right == None:
            return True
        if left == None or right == None:
            return False
        if left.data != right.data:
            return False
        
        return (self.solve(left.left,right.right) and self.solve(left.right,right.left))    
                
0
vivekroxstar92 weeks ago
Javascript solution 
class Solution {
  	// return true/false denoting whether the tree is Symmetric or not
  	symmetric(root1,root2){
  	    if(!root1 && !root2){
  	        return true;
  	    }
  	    if(!root1 || !root2){
  	        return false;
  	    }
  	    return (root1.key==root2.key) && this.symmetric(root1.left,root2.right)&&this.symmetric(root1.right,root2.left);
  	}
    isSymmetric(root){
        //code here
        return this.symmetric(root,root);
    }
}
+2
salim2hbk2 weeks ago
BFS implementation:
 
public static boolean isSymmetric(Node node)   {       LinkedList<Node> que1 = new LinkedList<>();       LinkedList<Node> que2 = new LinkedList<>();       que1.addLast(node);       que2.addLast(node);       while (!que1.isEmpty()) {           Node node1 = que1.removeFirst();           Node node2 = que2.removeFirst();
           if(node1 == null && node2==null){               continue;           }
           if(node1!=null && node2==null){               return false;           }           if(node1==null && node2!=null){               return false;           }           if(node1.data != node2.data){               return false;           }
           que1.addLast(node1.left);           que2.addLast(node2.right);                 }
       return true;   }
0
kumaarsahab4323 weeks ago
bool solve(Node* left, Node* right)
    {
        if(left==NULL and right==NULL) return true ;
        if(left==NULL or right==NULL) return false ;
        if(left->data!=right->data) return false ;
        return solve(left->left,right->right) and solve(left->right,right->left) ;
    }
    bool isSymmetric(struct Node* root)
    {
	    if(root==NULL) return true ;
	    return solve(root->left,root->right) ;
    }
0
himanshujain4573 weeks ago
Simple Solution:
class GfG{   // return true/false denoting whether the tree is Symmetric or not  static void solve(Node r,ArrayList<Integer>left){       if(r==null)return;       left.add(r.data);       solve(r.left,left);       solve(r.right,left);   }    static void solve1(Node r,ArrayList<Integer>left){       if(r==null)return;       left.add(r.data);       solve1(r.right,left);       solve1(r.left,left);   }   public static boolean isSymmetric(Node root)   {       ArrayList<Integer>left=new ArrayList<Integer>();       ArrayList<Integer>right=new ArrayList<Integer>();       if(root==null||(root.left==null&&root.right==null))       return true;       solve(root.left,left);       solve1(root.right,right);       return left.equals(right);          }}
+1
prishita4 weeks ago
JAVA Solution
class GfG
{
    // return true/false denoting whether the tree is Symmetric or not
    public static boolean isSymmetric(Node root)
    {
        // add your code here;
        if(root==null)
            return true;
        return isSymmetric(root.left,root.right);
    }
    public static boolean isSymmetric(Node l,Node r){
        if(r==null && l==null)
            return true;
        else if(r==null || l==null)
            return false;
        if(l.data!=r.data) return false;
        
        if(!isSymmetric(l.left,r.right)) return false;
        if(!isSymmetric(l.right, r.left)) return false;

        return true;
    }
}
0
amishasahu3284 weeks ago
class Solution{
    public:
    bool check( struct Node* leftSubtree, struct Node* rightSubtree)
    {
        if(!leftSubtree || !rightSubtree)
            return (leftSubtree == rightSubtree);
        if(leftSubtree->data != rightSubtree->data)
            return false;
        return (check(leftSubtree->left, rightSubtree->right) && check(leftSubtree->right, rightSubtree->left));
    }
    bool isSymmetric(struct Node* root)
    {
	    return (root == NULL || check(root->left, root->right));
    }
};
+1
adarshgupta4014 weeks ago
Easy Java Soln
class GfG
{
    // return true/false denoting whether the tree is Symmetric or not
    public static boolean isSymmetric(Node root)
    {
        return root==null  || checkSymmetric( root.left , root.right);
    }
    private static boolean checkSymmetric(Node l, Node r){
        if(l== null || r==null) return l==r;
        if(l.data != r.data) return false;
        return checkSymmetric(l.left, r.right) && checkSymmetric(l.right , r.left);
    }
}
0
madhab poulik4 weeks ago
//easy approach c++
bool isSymmetricutil (struct Node* root1, struct Node* root2)    {    // Code here    if(!root1 && !root2)        return true;    if(!root1 || !root2)        return false;    if(root1->data != root2->data)        return false;    else         return isSymmetricutil(root1->left,root2->right) && isSymmetricutil(root1->right,root2->left);   }   bool isSymmetric(struct Node* root){       return isSymmetricutil(root, root);   }
+1
badgujarsachin831 month ago
 bool cheak(Node* l,Node* r){
        if(l==NULL && r==NULL){
            return 1;
        }
        if(l==NULL || r==NULL){
            return false;
        }
        else{
            bool a=l->data==r->data;
            bool left=cheak(l->left,r->right);
            bool right=cheak(l->left,r->right);
            return (a && left && right);
        }
    }
    // return true/false denoting whether the tree is Symmetric or not
    bool isSymmetric(struct Node* root)
    {
	   return cheak(root,root);
    }
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.