Tree Isomorphism Problem

Last Updated : 11 Aug, 2026

Given two Binary Trees, check whether they are isomorphic or not. Two trees are considered isomorphic if one can be transformed into the other by performing a series of flips, where a flip means swapping the left and right children of a node. These swaps can be applied to any number of nodes at any level.

Note:

  • If two trees are the same (same structure and node values), they are isomorphic.
  • Two empty trees are isomorphic.
  • If the root node values of both trees differ, they are not isomorphic.


Examples:

Input:

Tree-Isomorphism-Problem

Output: True
Explanation: The above two trees are isomorphic with following sub-trees flipped: 2 and 3, NULL and 6, 7 and 8. 

Try It Yourself
redirect icon

Using Recursion - O(n) Time and O(n) Space

The idea is to traverse both trees recursively, comparing the nodes n1 and n2. Their data must be the same, and their subtrees must either be identical or mirror images (flipped). This ensures that the trees are structurally isomorphic.

Let the current internal nodes of the two trees be n1 and n2. For the subtrees rooted at n1 and n2 to be isomorphic, the following conditions must hold:

  • The data of n1 and n2 must be the same.
  • One of the following two conditions must be true for the children of n1 and n2:
  • The left of n1 is isomorphic to the left of n2, and the right of n1 is isomorphic to the right of n2.
  • The left of n1 is isomorphic to the right of n2, and the right of n1 is isomorphic to the left of n2.
  • This ensures that the trees are either structurally identical or have been "flipped" at some levels while still being isomorphic.
C++
using namespace std;

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

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

// Function to check if two trees are isomorphic
bool isIsomorphic(Node *root1, Node *root2)
{

    // Both roots are NULL, trees are isomorphic
    // by definition
    if (root1 == nullptr && root2 == nullptr)
    {
        return true;
    }

    // Exactly one of the root1 and root2 is NULL,
    // trees not isomorphic
    if (root1 == nullptr || root2 == nullptr)
    {
        return false;
    }

    // If the data doesn't match, trees
    // are not isomorphic
    if (root1->data != root2->data)
    {
        return false;
    }

    // Check if the trees are isomorphic by
    // considering the two cases:
    // Case 1: The subtrees have not been flipped
    // Case 2: The subtrees have been flipped
    return (isIsomorphic(root1->left, root2->left) && isIsomorphic(root1->right, root2->right)) ||
           (isIsomorphic(root1->left, root2->right) && isIsomorphic(root1->right, root2->left));
}

int main()
{

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

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

    if (isIsomorphic(root1, root2))
    {
        cout << "True\n";
    }
    else
    {
        cout << "False\n";
    }

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

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

class GfG {

    // Function to check if two trees are isomorphic
    static boolean isIsomorphic(Node root1, Node root2)
    {

        // Both roots are NULL, trees are
        // isomorphic by definition
        if (root1 == null && root2 == null) {
            return true;
        }

        // Exactly one of the root1 and root2 is NULL,
        // trees not isomorphic
        if (root1 == null || root2 == null) {
            return false;
        }

        // If the data doesn't match, trees are not
        // isomorphic
        if (root1.data != root2.data) {
            return false;
        }

        // Check if the trees are isomorphic by
        // considering the two cases:
        // Case 1: The subtrees have not been flipped
        // Case 2: The subtrees have been flipped
        return (isIsomorphic(root1.left, root2.left)
                && isIsomorphic(root1.right, root2.right))
            || (isIsomorphic(root1.left, root2.right)
                && isIsomorphic(root1.right, root2.left));
    }

    public static void main(String[] args)
    {

        // Representation of input binary tree 1
        //         1
        //        / \
        //       2   3
        //      / \  /
        //     4   5 6
        //        / \
        //       7   8

        Node root1 = new Node(1);
        root1.left = new Node(2);
        root1.right = new Node(3);
        root1.left.left = new Node(4);
        root1.left.right = new Node(5);
        root1.right.left = new Node(6);
        root1.left.right.left = new Node(7);
        root1.left.right.right = new Node(8);

        // Representation of input binary tree 2
        //         1
        //        / \
        //       3   2
        //        \  / \
        //         6 4   5
        //               / \
        //              8   7

        Node root2 = new Node(1);
        root2.left = new Node(3);
        root2.right = new Node(2);
        root2.left.right = new Node(6);
        root2.right.left = new Node(4);
        root2.right.right = new Node(5);
        root2.right.right.left = new Node(8);
        root2.right.right.right = new Node(7);

        if (isIsomorphic(root1, root2)) {
            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


# Function to check if two trees are isomorphic
def isIsomorphic(root1, root2):

    # Both roots are None → isomorphic
    if root1 is None and root2 is None:
        return True

    # One is None → not isomorphic
    if root1 is None or root2 is None:
        return False

    # Data mismatch → not isomorphic
    if root1.data != root2.data:
        return False

    # Check both cases: no flip OR flip
    return (isIsomorphic(root1.left, root2.left) and
            isIsomorphic(root1.right, root2.right)) or \
           (isIsomorphic(root1.left, root2.right) and
            isIsomorphic(root1.right, root2.left))


if __name__ == "__main__":

    # Representation of input binary tree 1
    #         1
    #        / \
    #       2   3
    #      / \  /
    #     4   5 6
    #        / \
    #       7   8

    root1 = Node(1)
    root1.left = Node(2)
    root1.right = Node(3)
    root1.left.left = Node(4)
    root1.left.right = Node(5)
    root1.right.left = Node(6)
    root1.left.right.left = Node(7)
    root1.left.right.right = Node(8)

    # Representation of input binary tree 2
    #         1
    #        / \
    #       3   2
    #        \  / \
    #         6 4   5
    #               / \
    #              8   7

    root2 = Node(1)
    root2.left = Node(3)
    root2.right = Node(2)
    root2.left.right = Node(6)
    root2.right.left = Node(4)
    root2.right.right = Node(5)
    root2.right.right.left = Node(8)
    root2.right.right.right = Node(7)

    if isIsomorphic(root1, root2):
        print("True")
    else:
        print("False")
C#
using System;

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

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

class GfG {

    // Function to check if two trees are isomorphic
    static bool isIsomorphic(Node root1, Node root2)
    {

        // Both roots are null, trees are
        // isomorphic by definition
        if (root1 == null && root2 == null) {
            return true;
        }

        // Exactly one of the root1 and root2 is null,
        // trees not isomorphic
        if (root1 == null || root2 == null) {
            return false;
        }

        // If the data doesn't match, trees are not
        // isomorphic
        if (root1.data != root2.data) {
            return false;
        }

        // Check if the trees are isomorphic by
        // considering the two cases:
        // Case 1: The subtrees have not been flipped
        // Case 2: The subtrees have been flipped
        return (isIsomorphic(root1.left, root2.left)
                && isIsomorphic(root1.right, root2.right))
            || (isIsomorphic(root1.left, root2.right)
                && isIsomorphic(root1.right, root2.left));
    }

    static void Main(string[] args)
    {

        // Representation of input binary tree 1
        //         1
        //        / \
        //       2   3
        //      / \  /
        //     4   5 6
        //        / \
        //       7   8

        Node root1 = new Node(1);
        root1.left = new Node(2);
        root1.right = new Node(3);
        root1.left.left = new Node(4);
        root1.left.right = new Node(5);
        root1.right.left = new Node(6);
        root1.left.right.left = new Node(7);
        root1.left.right.right = new Node(8);

        // Representation of input binary tree 2
        //         1
        //        / \
        //       3   2
        //        \  / \
        //         6 4   5
        //               / \
        //              8   7

        Node root2 = new Node(1);
        root2.left = new Node(3);
        root2.right = new Node(2);
        root2.left.right = new Node(6);
        root2.right.left = new Node(4);
        root2.right.right = new Node(5);
        root2.right.right.left = new Node(8);
        root2.right.right.right = new Node(7);

        if (isIsomorphic(root1, root2)) {
            Console.WriteLine("True");
        }
        else {
            Console.WriteLine("False");
        }
    }
}
JavaScript
class Node {
    constructor(x) {
        this.data = x;
        this.left = null;
        this.right = null;
    }
}

// Function to check if two trees are isomorphic
function isIsomorphic(root1, root2) {

    // Both roots are null, trees are isomorphic
    // by definition
    if (root1 === null && root2 === null) {
        return true;
    }

    // Exactly one of the root1 and root2 is null,
    // trees not isomorphic
    if (root1 === null || root2 === null) {
        return false;
    }

    // If the data doesn't match, trees are not isomorphic
    if (root1.data !== root2.data) {
        return false;
    }

    // Check if the trees are isomorphic by 
    // considering the two cases:
    // Case 1: The subtrees have not been flipped
    // Case 2: The subtrees have been flipped
    return (isIsomorphic(root1.left, root2.left) &&
            isIsomorphic(root1.right, root2.right)) ||
           (isIsomorphic(root1.left, root2.right) &&
            isIsomorphic(root1.right, root2.left));
}

// Representation of input binary tree 1
//         1
//        / \
//       2   3
//      / \  /
//     4   5 6
//        / \
//       7   8

let root1 = new Node(1);
root1.left = new Node(2);
root1.right = new Node(3);
root1.left.left = new Node(4);
root1.left.right = new Node(5);
root1.right.left = new Node(6);
root1.left.right.left = new Node(7);
root1.left.right.right = new Node(8);

// Representation of input binary tree 2
//         1
//        / \
//       3   2
//        \  / \
//         6 4  5
//              / \
//             8   7

let root2 = new Node(1);
root2.left = new Node(3);
root2.right = new Node(2);
root2.left.right = new Node(6);
root2.right.left = new Node(4);
root2.right.right = new Node(5);
root2.right.right.left = new Node(8);
root2.right.right.right = new Node(7);

if (isIsomorphic(root1, root2)) {
    console.log("True");
} else {
    console.log("False");
}

Output
True

Using Iteration - O(n) Time and O(n) Space

The idea is to traverse the first tree using level-order traversal (BFS) and store all parent-child relationships in a map. Each entry in the map represents a pair of (child node value, parent node value).

After that, we traverse the second tree and verify whether every such parent-child relationship exists in the map. If any relationship is missing, the trees cannot be isomorphic.

This approach works because even if we flip nodes (swap left and right children), the parent-child relationship remains unchanged, only their positions change.

Example: Consider the following two Binary trees, T1 and T2:

Step 1: Traverse Tree 1 (Level-order BFS)

We traverse the first tree level by level and record all parent-child pairs in a map or set.

420047146
  • Parent-Child Pairs (child,parent): (2,1), (3,1), (4,2), (5,2), (6,3), (7,5), (8,5)
  • Each pair consists of a child node value and its parent node value; the order of children doesn’t matter, only the parent-child relationship does.

Step 2: Traverse Tree 2 and check pairs

420047147
  • For Tree 2, the generated parent-child pairs are (3,1), (2,1), (6,3), (4,2), (5,2), (8,5), (7,5).
  • By checking each pair against the map from Tree 1, we find that all pairs exist, confirming that the trees are isomorphic.

Step 3: Why this works with flips

  • Left and right children can be swapped; parent-child pairs remain unchanged.
  • Example: Node 5 - children (7,8) in Tree 1; (8,7) in Tree 2.
  • Order of children doesn’t matter.
  • BFS + parent-child pairs preserve tree structure despite flips.
C++
using namespace std;

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

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

// Helper function to check if the second
// tree is isomorphic
bool CheckTree(Node *root2, map<pair<int, int>, bool> &visited) {

    // If root of the second tree is not visited
    if (visited[{root2->data, -1}] == false) {
        return false;
    }

    queue<Node *> q;
    q.push(root2);

    // Traverse the second tree
    while (!q.empty()) {
        Node *curr = q.front();
        q.pop();

        if (curr->left) {

            // If the left child has not been visited
            if (visited[{curr->left->data, curr->data}] == false) {
                return false;
            }
            q.push(curr->left);
        }
        if (curr->right) {

            // If the right child has not been visited
            if (visited[{curr->right->data, curr->data}] == false) {
                return false;
            }
            q.push(curr->right);
        }
    }
    return true;
}

// Main function to check if two trees are isomorphic
bool isIsomorphic(Node *root1, Node *root2) {
    map<pair<int, int>, bool> visited;

    // Mark the root of the first tree as visited
    visited[{root1->data, -1}] = true;

    queue<Node *> q;
    q.push(root1);

    // Traverse the first tree and mark nodes as visited
    while (!q.empty()) {
        Node *curr = q.front();
        q.pop();

        if (curr->left) {
            visited[{curr->left->data, curr->data}] = true;
            q.push(curr->left);
        }
        if (curr->right) {
            visited[{curr->right->data, curr->data}] = true;
            q.push(curr->right);
        }
    }

    // Use the helper function to check the second tree
    return CheckTree(root2, visited);
}

int main() {

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

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

    if (isIsomorphic(root1, root2)) {
        cout << "True\n";
    }
    else {
        cout << "False\n";
    }

    return 0;
}
Java
import java.util.Queue;
import java.util.LinkedList;
import java.util.Map;
import java.util.HashMap;
import java.util.Objects;

class Node {
    int data;
    Node left, right;

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

// Pair class to store (child, parent)
class Pair {
    int first, second;

    Pair(int first, int second) {
        this.first = first;
        this.second = second;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null || getClass() != obj.getClass())
            return false;
        Pair p = (Pair) obj;
        return first == p.first && second == p.second;
    }

    @Override
    public int hashCode() {
        return Objects.hash(first, second);
    }
}

class GFG {

    // Helper function to check if the second
    // tree is isomorphic
    static boolean CheckTree(Node root2,
                             Map<Pair, Boolean> visited) {

        // If root of the second tree is not visited
        if (!visited.containsKey(new Pair(root2.data, -1))) {
            return false;
        }

        Queue<Node> q = new LinkedList<>();
        q.add(root2);

        // Traverse the second tree
        while (!q.isEmpty()) {
            Node curr = q.poll();

            if (curr.left != null) {

                // If the left child has not been visited
                if (!visited.containsKey(
                        new Pair(curr.left.data, curr.data))) {
                    return false;
                }
                q.add(curr.left);
            }

            if (curr.right != null) {

                // If the right child has not been visited
                if (!visited.containsKey(
                        new Pair(curr.right.data, curr.data))) {
                    return false;
                }
                q.add(curr.right);
            }
        }
        return true;
    }

    // Main function to check if two trees are isomorphic
    static boolean isIsomorphic(Node root1, Node root2) {

        Map<Pair, Boolean> visited = new HashMap<>();

        // Mark the root of the first tree as visited
        visited.put(new Pair(root1.data, -1), true);

        Queue<Node> q = new LinkedList<>();
        q.add(root1);

        // Traverse the first tree and mark nodes as visited
        while (!q.isEmpty()) {
            Node curr = q.poll();

            if (curr.left != null) {
                visited.put(
                    new Pair(curr.left.data, curr.data), true);
                q.add(curr.left);
            }

            if (curr.right != null) {
                visited.put(
                    new Pair(curr.right.data, curr.data), true);
                q.add(curr.right);
            }
        }

        // Use the helper function to check the second tree
        return CheckTree(root2, visited);
    }

    public static void main(String[] args) {

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

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

        if (isIsomorphic(root1, root2)) {
            System.out.println("True");
        } else {
            System.out.println("False");
        }
    }
}
Python
from collections import deque

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

# Helper function to check if the second tree is isomorphic
def check_tree(root2, visited):
    if (root2.data, -1) not in visited:
        return False

    queue = [root2]

    while queue:
        curr = queue.pop(0)

        if curr.left:
            if (curr.left.data, curr.data) not in visited:
                return False
            queue.append(curr.left)

        if curr.right:
            if (curr.right.data, curr.data) not in visited:
                return False
            queue.append(curr.right)

    return True

# Main function to check if two trees are isomorphic
def isIsomorphic(root1, root2):
    visited = set()

    # Mark the root of the first tree as visited
    visited.add((root1.data, -1))

    queue = [root1]

    # Traverse the first tree and mark nodes as visited
    while queue:
        curr = queue.pop(0)

        if curr.left:
            visited.add((curr.left.data, curr.data))
            queue.append(curr.left)

        if curr.right:
            visited.add((curr.right.data, curr.data))
            queue.append(curr.right)

    # Use the helper function to check the second tree
    return check_tree(root2, visited)


if __name__ == "__main__":

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

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

    if isIsomorphic(root1, root2):
        print("True")
    else:
        print("False")
C#
using System;
using System.Collections.Generic;

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

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

// Pair class to store (child, parent)
class Pair
{
    public int First, Second;

    public Pair(int first, int second)
    {
        First = first;
        Second = second;
    }

    public override bool Equals(object obj)
    {
        if (this == obj) return true;
        if (obj == null || obj.GetType() != GetType()) return false;
        Pair p = (Pair)obj;
        return First == p.First && Second == p.Second;
    }

    public override int GetHashCode()
    {
        return HashCode.Combine(First, Second);
    }
}

class GFG
{
    // Helper function to check if the second tree is isomorphic
    static bool CheckTree(Node root2, Dictionary<Pair, bool> visited)
    {
        // If root of the second tree is not visited
        if (!visited.ContainsKey(new Pair(root2.data, -1)))
            return false;

        Queue<Node> q = new Queue<Node>();
        q.Enqueue(root2);

        // Traverse the second tree
        while (q.Count > 0)
        {
            Node curr = q.Dequeue();

            if (curr.left != null)
            {
                // If the left child has not been visited
                if (!visited.ContainsKey(new Pair(curr.left.data, curr.data)))
                    return false;
                q.Enqueue(curr.left);
            }

            if (curr.right != null)
            {
                // If the right child has not been visited
                if (!visited.ContainsKey(new Pair(curr.right.data, curr.data)))
                    return false;
                q.Enqueue(curr.right);
            }
        }

        return true;
    }

    // Main function to check if two trees are isomorphic
    static bool isIsomorphic(Node root1, Node root2)
    {
        Dictionary<Pair, bool> visited = new Dictionary<Pair, bool>();

        // Mark the root of the first tree as visited
        visited[new Pair(root1.data, -1)] = true;

        Queue<Node> q = new Queue<Node>();
        q.Enqueue(root1);

        // Traverse the first tree and mark nodes as visited
        while (q.Count > 0)
        {
            Node curr = q.Dequeue();

            if (curr.left != null)
            {
                visited[new Pair(curr.left.data, curr.data)] = true;
                q.Enqueue(curr.left);
            }

            if (curr.right != null)
            {
                visited[new Pair(curr.right.data, curr.data)] = true;
                q.Enqueue(curr.right);
            }
        }

        // Use the helper function to check the second tree
        return CheckTree(root2, visited);
    }

    static void Main(string[] args)
    {
        // Representation of input binary tree 1
        //        1
        //       / \
        //      2   3
        //     / \  /
        //    4   5 6
        //       / \
        //      7   8
        Node root1 = new Node(1);
        root1.left = new Node(2);
        root1.right = new Node(3);
        root1.left.left = new Node(4);
        root1.left.right = new Node(5);
        root1.right.left = new Node(6);
        root1.left.right.left = new Node(7);
        root1.left.right.right = new Node(8);

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

        if (isIsomorphic(root1, root2))
            Console.WriteLine("True");
        else
            Console.WriteLine("False");
    }
}
JavaScript
class Node {
    constructor(data)
    {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}

// Pair class to store (child, parent)
class Pair {
    constructor(first, second)
    {
        this.first = first;
        this.second = second;
    }

    // Define equality for Map keys
    equals(other)
    {
        return other instanceof Pair
               && this.first === other.first
               && this.second === other.second;
    }

    // Generate string key for Map
    toString() { return `${this.first},${this.second}`; }
}

// Helper function to check if the second tree is isomorphic
function checkTree(root2, visited)
{
    if (!visited.has(new Pair(root2.data, -1).toString())) {
        return false;
    }

    const queue = [ root2 ];

    while (queue.length > 0) {
        const curr = queue.shift();

        if (curr.left) {
            if (!visited.has(
                    new Pair(curr.left.data, curr.data)
                        .toString())) {
                return false;
            }
            queue.push(curr.left);
        }

        if (curr.right) {
            if (!visited.has(
                    new Pair(curr.right.data, curr.data)
                        .toString())) {
                return false;
            }
            queue.push(curr.right);
        }
    }

    return true;
}

// Main function to check if two trees are isomorphic
function isIsomorphic(root1, root2)
{
    const visited = new Map();

    // Mark the root of the first tree as visited
    visited.set(new Pair(root1.data, -1).toString(), true);

    const queue = [ root1 ];

    // Traverse the first tree and mark nodes as visited
    while (queue.length > 0) {
        const curr = queue.shift();

        if (curr.left) {
            visited.set(new Pair(curr.left.data, curr.data)
                            .toString(),
                        true);
            queue.push(curr.left);
        }

        if (curr.right) {
            visited.set(new Pair(curr.right.data, curr.data)
                            .toString(),
                        true);
            queue.push(curr.right);
        }
    }

    // Use the helper function to check the second tree
    return checkTree(root2, visited);
}


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

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

console.log(isIsomorphic(root1, root2) ? "True" : "False");

Output
True
Comment