Open In App

POTD Solutions | 22 Nov’ 23 | Symmetric Tree

Last Updated : 24 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

View all POTD Solutions

Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Binary Tree but will also help you build up problem-solving skills.

22nd-november

POTD Solution 22 November 2023

We recommend you to try this problem on our GeeksforGeeks Practice portal first, and maintain your streak to earn Geeksbits and other exciting prizes, before moving towards the solution.
POTD 22th Nov

POTD 22 November: Symmetric Tree:

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:

Input:
5
/ \
3 3
/ \ / \
8 9 9 8

Output:
True
Explanation:
Tree is mirror image of itself i.e. tree is symmetric

Input:

5
/ \
8 7
\ \
4 3

Output: False

Check for Symmetric Tree using Morris Traversal:

The idea is to traverse the tree using Morris Traversal and Reverse Morris Traversal to traverse the given binary tree and at each step check that the data of the current node is equal in both the traversals. If at any step the data of the nodes are different. Then, the given tree is not Symmetric Binary Tree.

Below is the implementation of the above approach:

C++




class Solution {
public:
    // return true/false denoting whether the tree is
    // Symmetric or not
    bool isSymmetric(struct Node* root)
    {
        // Code here
        Node *curr1 = root, *curr2 = root;
  
        // Loop to traverse the tree in
        // Morris Traversal and
        // Reverse Morris Traversal
        while (curr1 != NULL && curr2 != NULL) {
  
            if (curr1->left == NULL
                && curr2->right == NULL) {
  
                if (curr1->data != curr2->data)
                    return false;
  
                curr1 = curr1->right;
                curr2 = curr2->left;
            }
  
            else if (curr1->left != NULL
                     && curr2->right != NULL) {
  
                Node* pre1 = curr1->left;
                Node* pre2 = curr2->right;
  
                while (pre1->right != NULL
                       && pre1->right != curr1
                       && pre2->left != NULL
                       && pre2->left != curr2) {
                    pre1 = pre1->right;
                    pre2 = pre2->left;
                }
  
                if (pre1->right == NULL
                    && pre2->left == NULL) {
  
                    // Here, we are threading the Node
                    pre1->right = curr1;
                    pre2->left = curr2;
                    curr1 = curr1->left;
                    curr2 = curr2->right;
                }
  
                else if (pre1->right == curr1
                         && pre2->left == curr2) {
  
                    // Unthreading the nodes
                    pre1->right = NULL;
                    pre2->left = NULL;
  
                    if (curr1->data != curr2->data)
                        return false;
                    curr1 = curr1->right;
                    curr2 = curr2->left;
                }
                else
                    return false;
            }
            else
                return false;
        }
  
        if (curr1 != curr2)
            return false;
  
        return true;
    }
};


Java




class GfG {
    // return true/false denoting whether the tree is
    // Symmetric or not
    public static boolean isSymmetric(Node root)
    {
        // add your code here;
        Node curr1 = root, curr2 = root;
  
        // Loop to traverse the tree in
        // Morris Traversal and
        // Reverse Morris Traversal
        while (curr1 != null && curr2 != null) {
  
            if (curr1.left == null && curr2.right == null) {
  
                if (curr1.data != curr2.data)
                    return false;
  
                curr1 = curr1.right;
                curr2 = curr2.left;
            }
  
            else if (curr1.left != null
                     && curr2.right != null) {
                Node pre1 = curr1.left;
                Node pre2 = curr2.right;
  
                while (pre1.right != null
                       && pre1.right != curr1
                       && pre2.left != null
                       && pre2.left != curr2) {
                    pre1 = pre1.right;
                    pre2 = pre2.left;
                }
  
                if (pre1.right == null
                    && pre2.left == null) {
  
                    // Here, we are threading the Node
                    pre1.right = curr1;
                    pre2.left = curr2;
                    curr1 = curr1.left;
                    curr2 = curr2.right;
                }
  
                else if (pre1.right == curr1
                         && pre2.left == curr2) {
  
                    // Unthreading the nodes
                    pre1.right = null;
                    pre2.left = null;
  
                    if (curr1.data != curr2.data)
                        return false;
                    curr1 = curr1.right;
                    curr2 = curr2.left;
                }
                else
                    return false;
            }
            else
                return false;
        }
  
        if (curr1 != curr2)
            return false;
  
        return true;
    }
}


Python3




class Solution:
    # return true/false denoting whether the tree is Symmetric or not
    def isSymmetric(self, root):
        # Your Code Here
        curr1 = root
        curr2 = root
  
        # Loop to traverse the tree in
        # Morris Traversal and
        # Reverse Morris Traversal
        while curr1 != None and curr2 != None:
  
            if (curr1.left == None and
                    curr2.right == None):
  
                if curr1.data != curr2.data:
                    return False
  
                curr1 = curr1.right
                curr2 = curr2.left
  
            elif curr1.left != None and curr2.right != None:
                pre1 = curr1.left
                pre2 = curr2.right
  
                while (pre1.right != None and
                       pre1.right != curr1 and
                       pre2.left != None and
                       pre2.left != curr2):
                    pre1 = pre1.right
                    pre2 = pre2.left
  
                if pre1.right == None and pre2.left == None:
  
                    # Here, we are threading the Node
                    pre1.right = curr1
                    pre2.left = curr2
                    curr1 = curr1.left
                    curr2 = curr2.right
  
                elif (pre1.right == curr1 and
                      pre2.left == curr2):
  
                    # Unthreading the nodes
                    pre1.right = None
                    pre2.left = None
  
                    if curr1.data != curr2.data:
                        return False
  
                    curr1 = curr1.right
                    curr2 = curr2.left
                else:
                    return False
  
            else:
                return False
  
        if curr1 != curr2:
            return False
  
        return True


Time Complexity: O(N), where N is the number of nodes in the binary tree.
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads