Check if two trees are Mirror

Last Updated : 24 Aug, 2026

Given two binary trees, a and b, check whether the two trees are mirror images of each other. Two binary trees are mirror images if:

  • Their root nodes have the same value.
  • The left subtree of the first tree is the mirror of the right subtree of the second tree.
  • The right subtree of the first tree is the mirror of the left subtree of the second tree.

Example:

Input: a[] = [1, 3, 2, N, N, 5, 4], b[] = [1, 2, 3, 4, 5, N, N]

frame_3298

Output: true
Explanation: Both trees have the same values and opposite subtree structures, so they are mirror images.

Input: a = [1, 2, 3], b = [1, 2, 4]

frame_3299

Output: false
Explanation: The root values are the same, but the corresponding nodes 3 and 4 have different values. Therefore, the two trees are not mirror images of each other.

Try It Yourself
redirect icon

Recursive Approach - O(n) Time and O(h) Space

Two binary trees are mirror images if their corresponding nodes have the same values and their left and right subtrees appear in opposite positions.

Suppose we are comparing nodes a and b. There are three cases:

  • If both nodes are NULL, there is nothing left to compare, so they are mirrors.
  • If only one node is NULL, the tree structures are different, so they are not mirrors.
  • If both nodes exist, their values must be equal. If the values are different, the trees are not mirrors. Otherwise, recursively compare their opposite subtrees.

For every pair of nodes:

  • Compare a->left with b->right.
  • Compare a->right with b->left.

The trees are mirrors only if both opposite subtree comparisons return true.

C++
#include <iostream>
using namespace std;

class Node {
  public:
    int data;
    Node* left;
    Node* right;

    Node(int x) {
        data = x;
        left = right = nullptr;
    }
};

bool areMirror(Node* a, Node* b) {

    // If both roots are NULL, they are mirrors
    if (a == nullptr && b == nullptr)
        return true;

    // If only one root is NULL, they are not mirrors
    if (a == nullptr || b == nullptr)
        return false;

    // If values are different, they are not mirrors
    if (a->data != b->data)
        return false;

    // Compare opposite subtrees
    bool left = areMirror(a->left, b->right);
    bool right = areMirror(a->right, b->left);

    // Both subtree comparisons must be true
    return left && right;
}

int main() {

    // Representation of input binary tree 1
    //        1
    //       / \
    //      3   2
    //          / \
    //         5   4
    Node* a = new Node(1);
    a->left = new Node(3);
    a->right = new Node(2);
    a->right->left = new Node(5);
    a->right->right = new Node(4);

    // Representation of input binary tree 2 (mirror)
    //        1
    //       / \
    //      2   3
    //     / \
    //    4   5
    Node* b = new Node(1);
    b->left = new Node(2);
    b->right = new Node(3);
    b->left->left = new Node(4);
    b->left->right = new Node(5);

    if (areMirror(a, b))
        cout << "true\n";
    else
        cout << "false\n";

    return 0;
}
Java
class Node {
    int data;
    Node left;
    Node right;

    Node(int x) {
        data = x;
        left = right = null;
    }
}

class GFG {

    static boolean areMirror(Node a, Node b) {

        // If both roots are NULL, they are mirrors
        if (a == null && b == null)
            return true;

        // If only one root is NULL, they are not mirrors
        if (a == null || b == null)
            return false;

        // If values are different, they are not mirrors
        if (a.data != b.data)
            return false;

        // Compare opposite subtrees
        boolean left = areMirror(a.left, b.right);
        boolean right = areMirror(a.right, b.left);

        // Both subtree comparisons must be true
        return left && right;
    }

    public static void main(String[] args) {

        // Representation of input binary tree 1
        //        1
        //       / \
        //      3   2
        //          / \
        //         5   4
        Node a = new Node(1);
        a.left = new Node(3);
        a.right = new Node(2);
        a.right.left = new Node(5);
        a.right.right = new Node(4);

        // Representation of input binary tree 2 (mirror)
        //        1
        //       / \
        //      2   3
        //     / \
        //    4   5
        Node b = new Node(1);
        b.left = new Node(2);
        b.right = new Node(3);
        b.left.left = new Node(4);
        b.left.right = new Node(5);

        if (areMirror(a, b))
            System.out.println("true");
        else
            System.out.println("false");
    }
}
Python
class Node:
    def __init__(self, x):
        self.data = x
        self.left = None
        self.right = None


def areMirror(a, b):

    # If both roots are NULL, they are mirrors
    if a is None and b is None:
        return True

    # If only one root is NULL, they are not mirrors
    if a is None or b is None:
        return False

    # If values are different, they are not mirrors
    if a.data != b.data:
        return False

    # Compare opposite subtrees
    left = areMirror(a.left, b.right)
    right = areMirror(a.right, b.left)

    # Both subtree comparisons must be true
    return left and right


if __name__ == "__main__":

    # Representation of input binary tree 1
    #        1
    #       / \
    #      3   2
    #          / \
    #         5   4
    a = Node(1)
    a.left = Node(3)
    a.right = Node(2)
    a.right.left = Node(5)
    a.right.right = Node(4)

    # Representation of input binary tree 2 (mirror)
    #        1
    #       / \
    #      2   3
    #     / \
    #    4   5
    b = Node(1)
    b.left = Node(2)
    b.right = Node(3)
    b.left.left = Node(4)
    b.left.right = Node(5)

    if areMirror(a, b):
        print("true")
    else:
        print("false")
C#
using System;

class Node {
    public int data;
    public Node left;
    public Node right;

    public Node(int x) {
        data = x;
        left = right = null;
    }
}

class GFG {

    static bool areMirror(Node a, Node b) {

        // If both roots are NULL, they are mirrors
        if (a == null && b == null)
            return true;

        // If only one root is NULL, they are not mirrors
        if (a == null || b == null)
            return false;

        // If values are different, they are not mirrors
        if (a.data != b.data)
            return false;

        // Compare opposite subtrees
        bool left = areMirror(a.left, b.right);
        bool right = areMirror(a.right, b.left);

        // Both subtree comparisons must be true
        return left && right;
    }

    static void Main() {

        // Representation of input binary tree 1
        //        1
        //       / \
        //      3   2
        //          / \
        //         5   4
        Node a = new Node(1);
        a.left = new Node(3);
        a.right = new Node(2);
        a.right.left = new Node(5);
        a.right.right = new Node(4);

        // Representation of input binary tree 2 (mirror)
        //        1
        //       / \
        //      2   3
        //     / \
        //    4   5
        Node b = new Node(1);
        b.left = new Node(2);
        b.right = new Node(3);
        b.left.left = new Node(4);
        b.left.right = new Node(5);

        if (areMirror(a, b))
            Console.WriteLine("true");
        else
            Console.WriteLine("false");
    }
}
JavaScript
class Node {
    constructor(x) {
        this.data = x;
        this.left = null;
        this.right = null;
    }
}

function areMirror(a, b) {

    // If both roots are NULL, they are mirrors
    if (a === null && b === null)
        return true;

    // If only one root is NULL, they are not mirrors
    if (a === null || b === null)
        return false;

    // If values are different, they are not mirrors
    if (a.data !== b.data)
        return false;

    // Compare opposite subtrees
    let left = areMirror(a.left, b.right);
    let right = areMirror(a.right, b.left);

    // Both subtree comparisons must be true
    return left && right;
}

// Driver code

// Representation of input binary tree 1
//        1
//       / \
//      3   2
//          / \
//         5   4
let a = new Node(1);
a.left = new Node(3);
a.right = new Node(2);
a.right.left = new Node(5);
a.right.right = new Node(4);

// Representation of input binary tree 2 (mirror)
//        1
//       / \
//      2   3
//     / \
//    4   5
let b = new Node(1);
b.left = new Node(2);
b.right = new Node(3);
b.left.left = new Node(4);
b.left.right = new Node(5);

if (areMirror(a, b))
    console.log("true");
else
    console.log("false");

Output
true

Iterative Approach Using Queue - O(n) Time and O(n) Space

The idea is to compare the two trees using a queue instead of recursion. For every pair of nodes, we compare their values and then add their opposite children to the queue.

For two nodes a and b:

  • If both are NULL, continue.
  • If only one is NULL, the trees are not mirrors.
  • If their values are different, the trees are not mirrors.
  • Otherwise, add the following pairs to the queue:
  • a->left with b->right
  • a->right with b->left

Continue until all corresponding pairs are checked. If every pair satisfies the mirror condition, return true.

C++
#include <iostream>
#include <queue>
using namespace std;

class Node {
  public:
    int data;
    Node* left;
    Node* right;

    Node(int x) {
        data = x;
        left = right = nullptr;
    }
};

bool areMirror(Node* a, Node* b) {
    queue<Node*> q;

    q.push(a);
    q.push(b);

    while (!q.empty()) {
        a = q.front();
        q.pop();

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

        // If both roots are NULL, they are mirrors
        if (a == nullptr && b == nullptr)
            continue;

        // If only one root is NULL, they are not mirrors
        if (a == nullptr || b == nullptr)
            return false;

        // If values are different, they are not mirrors
        if (a->data != b->data)
            return false;

        // Compare opposite subtrees
        q.push(a->left);
        q.push(b->right);

        q.push(a->right);
        q.push(b->left);
    }

    // All subtree comparisons are true
    return true;
}

int main() {

    // Representation of input binary tree 1
    //        1
    //       / \
    //      3   2
    //          / \
    //         5   4
    Node* a = new Node(1);
    a->left = new Node(3);
    a->right = new Node(2);
    a->right->left = new Node(5);
    a->right->right = new Node(4);

    // Representation of input binary tree 2 (mirror)
    //        1
    //       / \
    //      2   3
    //     / \
    //    4   5
    Node* b = new Node(1);
    b->left = new Node(2);
    b->right = new Node(3);
    b->left->left = new Node(4);
    b->left->right = new Node(5);

    if (areMirror(a, b))
        cout << "true\n";
    else
        cout << "false\n";

    return 0;
}
Java
import java.util.LinkedList;
import java.util.Queue;

class Node {
    int data;
    Node left;
    Node right;

    Node(int x) {
        data = x;
        left = right = null;
    }
}

class GFG {

    static boolean areMirror(Node a, Node b) {
        Queue<Node> q = new LinkedList<>();

        q.add(a);
        q.add(b);

        while (!q.isEmpty()) {
            a = q.remove();
            b = q.remove();

            // If both roots are NULL, they are mirrors
            if (a == null && b == null)
                continue;

            // If only one root is NULL, they are not mirrors
            if (a == null || b == null)
                return false;

            // If values are different, they are not mirrors
            if (a.data != b.data)
                return false;

            // Compare opposite subtrees
            q.add(a.left);
            q.add(b.right);

            q.add(a.right);
            q.add(b.left);
        }

        // All subtree comparisons are true
        return true;
    }

    public static void main(String[] args) {

        // Representation of input binary tree 1
        //        1
        //       / \
        //      3   2
        //          / \
        //         5   4
        Node a = new Node(1);
        a.left = new Node(3);
        a.right = new Node(2);
        a.right.left = new Node(5);
        a.right.right = new Node(4);

        // Representation of input binary tree 2 (mirror)
        //        1
        //       / \
        //      2   3
        //     / \
        //    4   5
        Node b = new Node(1);
        b.left = new Node(2);
        b.right = new Node(3);
        b.left.left = new Node(4);
        b.left.right = new Node(5);

        if (areMirror(a, b))
            System.out.println("true");
        else
            System.out.println("false");
    }
}
Python
from collections import deque

class Node:
    def __init__(self, x):
        self.data = x
        self.left = None
        self.right = None


def areMirror(a, b):
    q = deque()

    q.append(a)
    q.append(b)

    while q:
        a = q.popleft()
        b = q.popleft()

        # If both roots are NULL, they are mirrors
        if a is None and b is None:
            continue

        # If only one root is NULL, they are not mirrors
        if a is None or b is None:
            return False

        # If values are different, they are not mirrors
        if a.data != b.data:
            return False

        # Compare opposite subtrees
        q.append(a.left)
        q.append(b.right)

        q.append(a.right)
        q.append(b.left)

    # All subtree comparisons are true
    return True

if __name__ == "__main__":

    # Representation of input binary tree 1
    #        1
    #       / \
    #      3   2
    #          / \
    #         5   4
    a = Node(1)
    a.left = Node(3)
    a.right = Node(2)
    a.right.left = Node(5)
    a.right.right = Node(4)

    # Representation of input binary tree 2 (mirror)
    #        1
    #       / \
    #      2   3
    #     / \
    #    4   5
    b = Node(1)
    b.left = Node(2)
    b.right = Node(3)
    b.left.left = Node(4)
    b.left.right = Node(5)

    if areMirror(a, b):
        print("true")
    else:
        print("false")
C#
using System;
using System.Collections.Generic;

public class Node {
    public int data;
    public Node left;
    public Node right;

    public Node(int x) {
        data = x;
        left = right = null;
    }
}

class GFG {

    public static bool areMirror(Node a, Node b) {
        Queue<Node> q = new Queue<Node>();

        q.Enqueue(a);
        q.Enqueue(b);

        while (q.Count > 0) {
            a = q.Dequeue();
            b = q.Dequeue();

            // If both roots are NULL, they are mirrors
            if (a == null && b == null)
                continue;

            // If only one root is NULL, they are not mirrors
            if (a == null || b == null)
                return false;

            // If values are different, they are not mirrors
            if (a.data != b.data)
                return false;

            // Compare opposite subtrees
            q.Enqueue(a.left);
            q.Enqueue(b.right);

            q.Enqueue(a.right);
            q.Enqueue(b.left);
        }

        // All subtree comparisons are true
        return true;
    }

    public static void Main() {

        // Representation of input binary tree 1
        //        1
        //       / \
        //      3   2
        //          / \
        //         5   4
        Node a = new Node(1);
        a.left = new Node(3);
        a.right = new Node(2);
        a.right.left = new Node(5);
        a.right.right = new Node(4);

        // Representation of input binary tree 2 (mirror)
        //        1
        //       / \
        //      2   3
        //     / \
        //    4   5
        Node b = new Node(1);
        b.left = new Node(2);
        b.right = new Node(3);
        b.left.left = new Node(4);
        b.left.right = new Node(5);

        if (areMirror(a, b))
            Console.WriteLine("true");
        else
            Console.WriteLine("false");
    }
}
JavaScript
class Node {
    constructor(x) {
        this.data = x;
        this.left = null;
        this.right = null;
    }
}

function areMirror(a, b) {
    let q = [];

    q.push(a);
    q.push(b);

    let front = 0;

    while (front < q.length) {
        a = q[front++];
        b = q[front++];

        // If both roots are NULL, they are mirrors
        if (a === null && b === null)
            continue;

        // If only one root is NULL, they are not mirrors
        if (a === null || b === null)
            return false;

        // If values are different, they are not mirrors
        if (a.data !== b.data)
            return false;

        // Compare opposite subtrees
        q.push(a.left);
        q.push(b.right);

        q.push(a.right);
        q.push(b.left);
    }

    // All subtree comparisons are true
    return true;
}

// Driver code

    // Representation of input binary tree 1
    //        1
    //       / \
    //      3   2
    //          / \
    //         5   4
    let a = new Node(1);
    a.left = new Node(3);
    a.right = new Node(2);
    a.right.left = new Node(5);
    a.right.right = new Node(4);

    // Representation of input binary tree 2 (mirror)
    //        1
    //       / \
    //      2   3
    //     / \
    //    4   5
    let b = new Node(1);
    b.left = new Node(2);
    b.right = new Node(3);
    b.left.left = new Node(4);
    b.left.right = new Node(5);

    if (areMirror(a, b))
        console.log("true");
    else
        console.log("false");

Output
true

Iterative Approach Using Two Stacks - O(n) Time and O(n) Auxiliary Space

The idea is to use two stacks to compare the nodes of both trees without recursion. One stack stores nodes from the first tree, and the other stores nodes from the second tree.

For every pair of nodes a and b:

  • If both nodes are NULL, continue.
  • If only one node is NULL, return false.
  • If their values are different, return false.
  • Push a->left and b->right into their respective stacks.
  • Push a->right and b->left into their respective stacks.

This ensures that the left subtree of one tree is always compared with the right subtree of the other tree. If their values are different, return false.

C++
#include <iostream>
#include <stack>
using namespace std;

class Node {
  public:
    int data;
    Node* left;
    Node* right;

    Node(int x) {
        data = x;
        left = right = nullptr;
    }
};

bool areMirror(Node* a, Node* b) {
    stack<Node*> s1, s2;

    s1.push(a);
    s2.push(b);

    while (!s1.empty() && !s2.empty()) {
        a = s1.top();
        s1.pop();

        b = s2.top();
        s2.pop();

        // If both roots are NULL, they are mirrors
        if (a == nullptr && b == nullptr)
            continue;

        // If only one root is NULL, they are not mirrors
        if (a == nullptr || b == nullptr)
            return false;

        // If values are different, they are not mirrors
        if (a->data != b->data)
            return false;

        // Compare opposite subtrees
        s1.push(a->left);
        s2.push(b->right);

        s1.push(a->right);
        s2.push(b->left);
    }

    // All subtree comparisons are true
    return s1.empty() && s2.empty();
}

int main() {

    // Representation of input binary tree 1
    //        1
    //       / \
    //      3   2
    //          / \
    //         5   4
    Node* a = new Node(1);
    a->left = new Node(3);
    a->right = new Node(2);
    a->right->left = new Node(5);
    a->right->right = new Node(4);

    // Representation of input binary tree 2 (mirror)
    //        1
    //       / \
    //      2   3
    //     / \
    //    4   5
    Node* b = new Node(1);
    b->left = new Node(2);
    b->right = new Node(3);
    b->left->left = new Node(4);
    b->left->right = new Node(5);

    if (areMirror(a, b))
        cout << "true\n";
    else
        cout << "false\n";

    return 0;
}
Java
import java.util.Stack;

class Node {
    int data;
    Node left;
    Node right;

    Node(int x) {
        data = x;
        left = right = null;
    }
}

class GFG {

    public static boolean areMirror(Node a, Node b) {
        Stack<Node> s1 = new Stack<>();
        Stack<Node> s2 = new Stack<>();

        s1.push(a);
        s2.push(b);

        while (!s1.empty() && !s2.empty()) {
            a = s1.pop();
            b = s2.pop();

            // If both roots are NULL, they are mirrors
            if (a == null && b == null)
                continue;

            // If only one root is NULL, they are not mirrors
            if (a == null || b == null)
                return false;

            // If values are different, they are not mirrors
            if (a.data != b.data)
                return false;

            // Compare opposite subtrees
            s1.push(a.left);
            s2.push(b.right);

            s1.push(a.right);
            s2.push(b.left);
        }

        // All subtree comparisons are true
        return s1.empty() && s2.empty();
    }

    public static void main(String[] args) {

        // Representation of input binary tree 1
        //        1
        //       / \
        //      3   2
        //          / \
        //         5   4
        Node a = new Node(1);
        a.left = new Node(3);
        a.right = new Node(2);
        a.right.left = new Node(5);
        a.right.right = new Node(4);

        // Representation of input binary tree 2 (mirror)
        //        1
        //       / \
        //      2   3
        //     / \
        //    4   5
        Node b = new Node(1);
        b.left = new Node(2);
        b.right = new Node(3);
        b.left.left = new Node(4);
        b.left.right = new Node(5);

        if (areMirror(a, b))
            System.out.println("true");
        else
            System.out.println("false");
    }
}
Python
class Node:
    def __init__(self, x):
        self.data = x
        self.left = None
        self.right = None


def areMirror(a, b):
    s1 = []
    s2 = []

    s1.append(a)
    s2.append(b)

    while s1 and s2:
        a = s1.pop()
        b = s2.pop()

        # If both roots are NULL, they are mirrors
        if a is None and b is None:
            continue

        # If only one root is NULL, they are not mirrors
        if a is None or b is None:
            return False

        # If values are different, they are not mirrors
        if a.data != b.data:
            return False

        # Compare opposite subtrees
        s1.append(a.left)
        s2.append(b.right)

        s1.append(a.right)
        s2.append(b.left)

    # All subtree comparisons are true
    return not s1 and not s2


if __name__ == "__main__":

    # Representation of input binary tree 1
    #        1
    #       / \
    #      3   2
    #          / \
    #         5   4
    a = Node(1)
    a.left = Node(3)
    a.right = Node(2)
    a.right.left = Node(5)
    a.right.right = Node(4)

    # Representation of input binary tree 2 (mirror)
    #        1
    #       / \
    #      2   3
    #     / \
    #    4   5
    b = Node(1)
    b.left = Node(2)
    b.right = Node(3)
    b.left.left = Node(4)
    b.left.right = Node(5)

    if areMirror(a, b):
        print("true")
    else:
        print("false")
C#
using System;
using System.Collections.Generic;

class Node {
    public int data;
    public Node left;
    public Node right;

    public Node(int x) {
        data = x;
        left = right = null;
    }
}

class GFG {

    static bool areMirror(Node a, Node b) {
        Stack<Node> s1 = new Stack<Node>();
        Stack<Node> s2 = new Stack<Node>();

        s1.Push(a);
        s2.Push(b);

        while (s1.Count > 0 && s2.Count > 0) {
            a = s1.Pop();
            b = s2.Pop();

            // If both roots are NULL, they are mirrors
            if (a == null && b == null)
                continue;

            // If only one root is NULL, they are not mirrors
            if (a == null || b == null)
                return false;

            // If values are different, they are not mirrors
            if (a.data != b.data)
                return false;

            // Compare opposite subtrees
            s1.Push(a.left);
            s2.Push(b.right);

            s1.Push(a.right);
            s2.Push(b.left);
        }

        // All subtree comparisons are true
        return s1.Count == 0 && s2.Count == 0;
    }

    static void Main() {

        // Representation of input binary tree 1
        //        1
        //       / \
        //      3   2
        //          / \
        //         5   4
        Node a = new Node(1);
        a.left = new Node(3);
        a.right = new Node(2);
        a.right.left = new Node(5);
        a.right.right = new Node(4);

        // Representation of input binary tree 2 (mirror)
        //        1
        //       / \
        //      2   3
        //     / \
        //    4   5
        Node b = new Node(1);
        b.left = new Node(2);
        b.right = new Node(3);
        b.left.left = new Node(4);
        b.left.right = new Node(5);

        if (areMirror(a, b))
            Console.WriteLine("true");
        else
            Console.WriteLine("false");
    }
}
JavaScript
class Node {
    constructor(x) {
        this.data = x;
        this.left = null;
        this.right = null;
    }
}

function areMirror(a, b) {
    let s1 = [];
    let s2 = [];

    s1.push(a);
    s2.push(b);

    while (s1.length > 0 && s2.length > 0) {
        a = s1.pop();
        b = s2.pop();

        // If both roots are NULL, they are mirrors
        if (a === null && b === null)
            continue;

        // If only one root is NULL, they are not mirrors
        if (a === null || b === null)
            return false;

        // If values are different, they are not mirrors
        if (a.data !== b.data)
            return false;

        // Compare opposite subtrees
        s1.push(a.left);
        s2.push(b.right);

        s1.push(a.right);
        s2.push(b.left);
    }

    // All subtree comparisons are true
    return s1.length === 0 && s2.length === 0;
}

// Driver code

    // Representation of input binary tree 1
    //        1
    //       / \
    //      3   2
    //          / \
    //         5   4
    let a = new Node(1);
    a.left = new Node(3);
    a.right = new Node(2);
    a.right.left = new Node(5);
    a.right.right = new Node(4);

    // Representation of input binary tree 2 (mirror)
    //        1
    //       / \
    //      2   3
    //     / \
    //    4   5
    let b = new Node(1);
    b.left = new Node(2);
    b.right = new Node(3);
    b.left.left = new Node(4);
    b.left.right = new Node(5);

    if (areMirror(a, b))
        console.log("true");
    else
        console.log("false");

Output
true
Comment