Check if two trees are mirror of each other using level order traversal

Given two binary trees, the task is to check whether the two binary trees is a mirror of each other or not.
Mirror of a Binary Tree: Mirror of a Binary Tree T is another Binary Tree M(T) with left and right children of all non-leaf nodes interchanged.

Trees in the above figure are mirrors of each other.



A recursive solution and an iterative method using inorder traversal to check whether the two binary trees is a mirror of each other or not have been already discussed. In this post a solution using level order traversal has been discussed.

The idea is to use a queue in which two nodes of both the trees which needs to be checked for equality are present together. At each step of level order traversal, get two nodes from the queue, check for their equality and then insert next two children nodes of these nodes which need to be checked for equality. During insertion step, first left child of first tree node and right child of second tree node are inserted. After this right child of first tree node and left child of second tree node are inserted. If at any stage one node is NULL and other is not, then both trees are not a mirror of each other.

Below is the implementation of above approach:

// C++ implementation to check whether the two
// binary tress are mirrors of each other or not
#include <bits/stdc++.h>
using namespace std;

// Structure of a node in binary tree
struct Node {
    int data;
    struct Node *left, *right;
};

// Function to create and return
// a new node for a binary tree
struct Node* newNode(int data)
{
    struct Node* temp = new Node();
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}

// Function to check whether the two binary trees
// are mirrors of each other or not
string areMirrors(Node* a, Node* b)
{
    // If both are NULL, then are mirror.
    if (a == NULL && b == NULL)
        return "Yes";

    // If only one is NULL, then not
    // mirror.
    if (a == NULL || b == NULL)
        return "No";

    queue<Node*> q;

    // Push root of both trees in queue.
    q.push(a);
    q.push(b);

    while (!q.empty()) {

        // Pop two elements of queue, to
        // get two nodes and check if they
        // are symmetric.
        a = q.front();
        q.pop();

        b = q.front();
        q.pop();

        // If data value of both nodes is
        // not same, then not mirror.
        if (a->data != b->data)
            return "No";

        // Push left child of first tree node
        // and right child of second tree node
        // into queue if both are not NULL.
        if (a->left && b->right) {
            q.push(a->left);
            q.push(b->right);
        }

        // If any one of the nodes is NULL and
        // other is not NULL, then not mirror.
        else if (a->left || b->right)
            return "No";

        // Push rigth child of first tree node
        // and left child of second tree node
        // into queue if both are not NULL.
        if (a->right && b->left) {
            q.push(a->right);
            q.push(b->left);
        }

        // If any one of the nodes is NULL and
        // other is not NULL, then not mirror.
        else if (a->right || b->left)
            return "No";
    }

    return "Yes";
}
// Driver Code
int main()
{
    // 1st binary tree formation
    /*
            1 
           / \ 
          3   2
             / \
            5   4
        */
    Node* root1 = newNode(1);
    root1->left = newNode(3);
    root1->right = newNode(2);
    root1->right->left = newNode(5);
    root1->right->right = newNode(4);

    // 2nd binary tree formation
    /*
            1 
           / \ 
          2   3
         / \
        4   5
        */
    Node* root2 = newNode(1);
    root2->left = newNode(2);
    root2->right = newNode(3);
    root2->left->left = newNode(4);
    root2->left->right = newNode(5);

    cout << areMirrors(root1, root2);
    return 0;
}
Output:

Yes

Time complexity: O(N)



My Personal Notes arrow_drop_up

A Programmer and A Machine learning Enthusiast

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




Practice Tags :

Recommended Posts:



2.5 Average Difficulty : 2.5/5.0
Based on 4 vote(s)






User Actions