Iterative method to check if two trees are mirror of each other
Last Updated : 23 Jul, 2025
Given two Binary Trees, the task is to check if two trees are mirrors 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.
The idea is to check if two binary trees are mirrors using two stacks to simulate the recursive process. Nodes from each tree are pushed onto the stacks in a manner that compares the left subtree of one tree with the right subtree of the other, and vice versa. By systematically comparing nodes from both trees while maintaining their mirrored structure in the stacks, the approach ensures that the trees are symmetric relative to their root.
Step-by-step implementation:
If both root1 and root2 are null, return true because two empty trees are mirrors of each other.
If one tree is null while the other isn't, return false, as they can't be mirrors.
Create two stacks, stk1 and stk2, to store nodes for simultaneous iterative traversal of both trees.
Pop nodes from both stacks, compare their data, and check if the current nodes have left and right children in a mirrored fashion (left of root1 with right of root2 and vice versa).
If the current node pairs have corresponding children, push them onto the stacks; otherwise, return false if the structure is not symmetric.
After traversal, if both stacks are empty, return true; otherwise, return false.
Below is implementation of above approach:
C++
// Iterative C++ program to check if two// roots are mirror images of each other#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left,*right;Node(intval){data=val;left=right=nullptr;}};// Function to check if two roots are mirror // images iterativelyboolareMirrors(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;// Create two stacks for simultaneous // traversalstack<Node*>stk1,stk2;// Push roots of both rootsstk1.push(root1);stk2.push(root2);while(!stk1.empty()&&!stk2.empty()){// Pop from both stacksNode*curr1=stk1.top();stk1.pop();Node*curr2=stk2.top();stk2.pop();// Check if the data of the nodes is // differentif(curr1->data!=curr2->data)returnfalse;// Check for the next level of nodes in // a mirror fashionif(curr1->left&&curr2->right){stk1.push(curr1->left);stk2.push(curr2->right);}elseif(curr1->left||curr2->right){returnfalse;}if(curr1->right&&curr2->left){stk1.push(curr1->right);stk2.push(curr2->left);}elseif(curr1->right||curr2->left){returnfalse;}}// If both stacks are empty, the roots are // mirrorsreturnstk1.empty()&&stk2.empty();}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;}
Java
// Iterative Java program to check if two// roots are mirror images of each otherimportjava.util.Stack;classNode{intdata;Nodeleft,right;Node(intval){data=val;left=right=null;}}publicclassGfG{// Function to check if two roots are // mirror images iterativelystaticbooleanareMirrors(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;}// Create two stacks for simultaneous // traversalStack<Node>stk1=newStack<>();Stack<Node>stk2=newStack<>();// Push roots of both rootsstk1.push(root1);stk2.push(root2);while(!stk1.isEmpty()&&!stk2.isEmpty()){// Pop from both stacksNodecurr1=stk1.pop();Nodecurr2=stk2.pop();// Check if the data of the nodes is // differentif(curr1.data!=curr2.data){returnfalse;}// Check for the next level of nodes // in a mirror fashionif(curr1.left!=null&&curr2.right!=null){stk1.push(curr1.left);stk2.push(curr2.right);}elseif(curr1.left!=null||curr2.right!=null){returnfalse;}if(curr1.right!=null&&curr2.left!=null){stk1.push(curr1.right);stk2.push(curr2.left);}elseif(curr1.right!=null||curr2.left!=null){returnfalse;}}// If both stacks are empty, the roots // are mirrorsreturnstk1.isEmpty()&&stk2.isEmpty();}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
# Iterative Python program to check if two# roots are mirror images of each otherclassNode:def__init__(self,val):self.data=valself.left=Noneself.right=NonedefareMirrors(root1,root2):# If both roots are empty, they are mirrorsifroot1isNoneandroot2isNone:returnTrue# If only one root is empty, they are not mirrorsifroot1isNoneorroot2isNone:returnFalse# Initialize two stacks for simultaneous traversalstk1=[]stk2=[]# Push roots of both rootsstk1.append(root1)stk2.append(root2)whilestk1andstk2:# Pop from both stackscurr1=stk1.pop()curr2=stk2.pop()# Check if the data of the nodes is differentifcurr1.data!=curr2.data:returnFalse# Check for the next level of nodes in a mirror # fashionifcurr1.leftandcurr2.right:stk1.append(curr1.left)stk2.append(curr2.right)elifcurr1.leftorcurr2.right:returnFalseifcurr1.rightandcurr2.left:stk1.append(curr1.right)stk2.append(curr2.left)elifcurr1.rightorcurr2.left:returnFalse# If both stacks are empty, the roots are mirrorsreturnnotstk1andnotstk2if__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)ifareMirrors(root1,root2):print("true")else:print("false")
C#
// Iterative C# program to check if two// roots are mirror images of each otherusingSystem;usingSystem.Collections.Generic;classNode{publicintdata;publicNodeleft,right;publicNode(intval){data=val;left=right=null;}}classGfG{// Function to check if two roots are // mirror images iterativelystaticboolAreMirrors(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;// Create two stacks for simultaneous // traversalStack<Node>stk1=newStack<Node>();Stack<Node>stk2=newStack<Node>();// Push roots of both rootsstk1.Push(root1);stk2.Push(root2);while(stk1.Count>0&&stk2.Count>0){// Pop from both stacksNodecurr1=stk1.Pop();Nodecurr2=stk2.Pop();// Check if the data of the nodes is // differentif(curr1.data!=curr2.data)returnfalse;// Check for the next level of nodes // in a mirror fashionif(curr1.left!=null&&curr2.right!=null){stk1.Push(curr1.left);stk2.Push(curr2.right);}elseif(curr1.left!=null||curr2.right!=null){returnfalse;}if(curr1.right!=null&&curr2.left!=null){stk1.Push(curr1.right);stk2.Push(curr2.left);}elseif(curr1.right!=null||curr2.left!=null){returnfalse;}}// If both stacks are empty, the roots // are mirrorsreturnstk1.Count==0&&stk2.Count==0;}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;}// Create two stacks for simultaneous traversalconststk1=[];conststk2=[];// Push roots of both rootsstk1.push(root1);stk2.push(root2);while(stk1.length>0&&stk2.length>0){// Pop from both stacksconstcurr1=stk1.pop();constcurr2=stk2.pop();// Check if the data of the nodes is differentif(curr1.data!==curr2.data){returnfalse;}// Check for the next level of nodes // in a mirror fashionif(curr1.left&&curr2.right){stk1.push(curr1.left);stk2.push(curr2.right);}elseif(curr1.left||curr2.right){returnfalse;}if(curr1.right&&curr2.left){stk1.push(curr1.right);stk2.push(curr2.left);}elseif(curr1.right||curr2.left){returnfalse;}}// If both stacks are empty, the roots are mirrorsreturnstk1.length===0&&stk2.length===0;}// 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), because we are visiting each node once, where n is the number of nodes in the trees. Auxiliary Space: O(n)