Given two Binary Trees, the task is to check if two trees are mirror of each other or not. For two trees ‘a’ and ‘b’ to be mirror images, the following three conditions must be true:
Their root node’s key must be same
Left subtree of root of ‘a’ and right subtree root of ‘b’ are mirror.
Right subtree of ‘a’ and left subtree of ‘b’ are mirror.
Example:
Input:
Output: True Explanation: Both trees are mirror images of each other, so output is True
Input:
Output: False Explanation: Since both trees are not mirror images of each other, the output is False.
[Expected Approach - 1] Recursive Approach - O(n) Time and O(h) Space
The idea is to check if two binary trees are mirrors of each other by comparing their structure and node values. We recursively verify if the root nodes of both trees have the same value, then check if the left subtree is a mirror of the right subtree. If both trees are empty, they are considered mirrors; if one is empty and the other is not, they are not mirrors. This approach ensures that the trees are symmetric with respect to their root.
Below is implementation of above approach:
C++
// Recursive C++ program to check if two // roots are mirror of each other#include<iostream>usingnamespacestd;classNode{public:intdata;Node*left,*right;Node(intval){data=val;left=right=nullptr;}};// Function to check if two roots are mirror imagesboolareMirrors(Node*root1,Node*root2){// If both roots are empty, they are mirrorsif(root1==nullptr&&root2==nullptr)returntrue;// If only one root is empty, they are not mirrorsif(root1==nullptr||root2==nullptr)returnfalse;// Check if the root data is the same and// if the left subroot of root1 is a mirror //of the right subroot of root2 and vice versareturn(root1->data==root2->data)&&areMirrors(root1->left,root2->right)&&areMirrors(root1->right,root2->left);}intmain(){// Representation of input binary tree 1// 1// / \ // 3 2// / \ // 5 4Node*root1=newNode(1);root1->left=newNode(3);root1->right=newNode(2);root1->right->left=newNode(5);root1->right->right=newNode(4);// Representation of input binary tree 2 (mirror)// 1// / \ // 2 3// / \ // 4 5Node*root2=newNode(1);root2->left=newNode(2);root2->right=newNode(3);root2->left->left=newNode(4);root2->left->right=newNode(5);if(areMirrors(root1,root2))cout<<"true\n";elsecout<<"false\n";return0;}
C
// Recursive C program to check if two // roots are mirror of each other#include<stdio.h>#include<stdbool.h>#include<stdlib.h>structNode{intdata;structNode*left,*right;};// Function to check if two roots are mirror imagesboolareMirrors(structNode*root1,structNode*root2){// If both roots are empty, they are mirrorsif(root1==NULL&&root2==NULL)returntrue;// If only one root is empty, they are not mirrorsif(root1==NULL||root2==NULL)returnfalse;// Check if the root data is the same and// if the left subroot of root1 is a mirror // of the right subroot of root2 and vice versareturn(root1->data==root2->data)&&areMirrors(root1->left,root2->right)&&areMirrors(root1->right,root2->left);}structNode*createNode(intval){structNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=val;newNode->left=newNode->right=NULL;returnnewNode;}intmain(){// Representation of input binary tree 1// 1// / \ // 3 2// / \ // 5 4structNode*root1=createNode(1);root1->left=createNode(3);root1->right=createNode(2);root1->right->left=createNode(5);root1->right->right=createNode(4);// Representation of input binary tree 2 (mirror)// 1// / \ // 2 3// / \ // 4 5structNode*root2=createNode(1);root2->left=createNode(2);root2->right=createNode(3);root2->left->left=createNode(4);root2->left->right=createNode(5);if(areMirrors(root1,root2))printf("true\n");elseprintf("false\n");return0;}
Java
// Recursive Java program to check if two// roots are mirror images of each otherclassNode{intdata;Nodeleft,right;Node(intval){data=val;left=right=null;}}publicclassGfG{// Function to check if two roots are mirror imagesstaticbooleanareMirrors(Noderoot1,Noderoot2){// If both roots are empty, they are mirrorsif(root1==null&&root2==null){returntrue;}// If only one root is empty, they are not mirrorsif(root1==null||root2==null){returnfalse;}// Check if the root data is the same and// if the left subroot of root1 is a mirror // of the right subroot of root2 and vice versareturn(root1.data==root2.data)&&areMirrors(root1.left,root2.right)&&areMirrors(root1.right,root2.left);}publicstaticvoidmain(String[]args){// Representation of input binary tree 1// 1// / \// 3 2// / \// 5 4Noderoot1=newNode(1);root1.left=newNode(3);root1.right=newNode(2);root1.right.left=newNode(5);root1.right.right=newNode(4);// Representation of input binary tree 2 (mirror)// 1// / \// 2 3// / \// 4 5Noderoot2=newNode(1);root2.left=newNode(2);root2.right=newNode(3);root2.left.left=newNode(4);root2.left.right=newNode(5);if(areMirrors(root1,root2)){System.out.println("true");}else{System.out.println("false");}}}
Python
# Recursive Python program to check if two# roots are mirror images of each otherclassNode:def__init__(self,val):self.data=valself.left=Noneself.right=Nonedefare_mirrors(root1,root2):# If both roots are empty, they are mirrorsifroot1isNoneandroot2isNone:returnTrue# If only one root is empty, they are not mirrorsifroot1isNoneorroot2isNone:returnFalse# Check if the root data is the same and# if the left subroot of root1 is a mirror # of the right subroot of root2 and vice versareturn(root1.data==root2.data)and \
are_mirrors(root1.left,root2.right)and \
are_mirrors(root1.right,root2.left)if__name__=="__main__":# Representation of input binary tree 1# 1# / \# 3 2# / \# 5 4root1=Node(1)root1.left=Node(3)root1.right=Node(2)root1.right.left=Node(5)root1.right.right=Node(4)# Representation of input binary tree 2 (mirror)# 1# / \# 2 3# / \# 4 5root2=Node(1)root2.left=Node(2)root2.right=Node(3)root2.left.left=Node(4)root2.left.right=Node(5)ifare_mirrors(root1,root2):print("true")else:print("false")
C#
// Recursive C# program to check if two// roots are mirror images of each otherusingSystem;classNode{publicintdata;publicNodeleft,right;publicNode(intval){data=val;left=right=null;}}classGfG{// Function to check if two roots are mirror imagesstaticboolAreMirrors(Noderoot1,Noderoot2){// If both roots are empty, they are mirrorsif(root1==null&&root2==null)returntrue;// If only one root is empty, they are not mirrorsif(root1==null||root2==null)returnfalse;// Check if the root data is the same and// if the left subroot of root1 is a mirror // of the right subroot of root2 and vice versareturn(root1.data==root2.data)&&AreMirrors(root1.left,root2.right)&&AreMirrors(root1.right,root2.left);}staticvoidMain(){// Representation of input binary tree 1// 1// / \// 3 2// / \// 5 4Noderoot1=newNode(1);root1.left=newNode(3);root1.right=newNode(2);root1.right.left=newNode(5);root1.right.right=newNode(4);// Representation of input binary tree 2 (mirror)// 1// / \// 2 3// / \// 4 5Noderoot2=newNode(1);root2.left=newNode(2);root2.right=newNode(3);root2.left.left=newNode(4);root2.left.right=newNode(5);if(AreMirrors(root1,root2))Console.WriteLine("true");elseConsole.WriteLine("false");}}
JavaScript
// Recursive JavaScript function to check if two// roots are mirror images of each otherclassNode{constructor(val){this.data=val;this.left=null;this.right=null;}}// Function to check if two roots are mirror imagesfunctionareMirrors(root1,root2){// If both roots are empty, they are mirrorsif(root1===null&&root2===null){returntrue;}// If only one root is empty, they are not mirrorsif(root1===null||root2===null){returnfalse;}// Check if the root data is the same and// if the left subroot of root1 is a mirror // of the right subroot of root2 and vice versareturn(root1.data===root2.data)&&areMirrors(root1.left,root2.right)&&areMirrors(root1.right,root2.left);}// Representation of input binary tree 1// 1// / \// 3 2// / \// 5 4constroot1=newNode(1);root1.left=newNode(3);root1.right=newNode(2);root1.right.left=newNode(5);root1.right.right=newNode(4);// Representation of input binary tree 2 (mirror)// 1// / \// 2 3// / \// 4 5constroot2=newNode(1);root2.left=newNode(2);root2.right=newNode(3);root2.left.left=newNode(4);root2.left.right=newNode(5);if(areMirrors(root1,root2)){console.log("true");}else{console.log("false");}
Output
true
Time Complexity:O(n), where n is the number of nodes in the trees. Auxiliary Space: O(h), where h is height of binary tree.
[Expected Approach - 2] Iterative Approach - O(n) Time and O(n) Space
The idea is to check if two binary trees are mirrors using two stacks to simulate recursion. Nodes from each tree are pushed onto the stacks in a way that compares the left subtree of one tree with the right subtree of the other, and vice versa. This approach systematically compares nodes while maintaining their mirrored structure, ensuring the trees are symmetric relative to their root. Please refer to Iterative method to check if two trees are mirror of each other for implementation.