Iterative Approach to check if two Binary Trees are Isomorphic or not

Given two Binary Trees we have to detect if the two trees are Isomorphic. Two trees are called isomorphic if one of them can be obtained from another by a series of flips, i.e. by swapping left and right children of a number of nodes. Any number of nodes at any level can have their children swapped.Â
Note: Two empty trees are isomorphic.
For example, the following two trees are isomorphic with the following sub-trees flipped: 2 and 3, NULL and 6, 7, and 8.
Approach:
To solve the question mentioned above we traverse both the trees iteratively using level order traversal and store the levels in a queue data structure. There are following two conditions at each level:Â Â
- The value of nodes has to be the same.
- The number of nodes at each level should be the same.
Check the size of the queue to match the second condition mentioned above. Store the nodes of each level of the first tree as key with a value and for the second tree, we will store all the nodes of a level in a vector. If the key is found we will decrease the value to keep track of how many nodes with the same value exists at a level. If the value becomes zero that means the first tree has only this much number of nodes, and we will remove it as a key. At the end of each level, we will iterate through the array and check whether each value exists on the map or not. There will be three conditions:Â
- If the key is not found then the first tree doesn’t contain a node found in the second tree at the same level.
- If the key is found but the value becomes negative then the second tree has more nodes with the same value as the first tree.
- If map size is not zero, this means there are some keys still left which means the first tree has a node that doesn’t match with any node in the second tree.
Below is the implementation of the above approach:
C++
// C++ program to find two// Binary Tree are Isomorphic or notÂ
#include <bits/stdc++.h>using namespace std;Â
/* A binary tree node has data,pointer to left and right children */struct node {Â Â Â Â int data;Â Â Â Â struct node* left;Â Â Â Â struct node* right;};Â
/* function to return if   tree are isomorphic or not*/bool isIsomorphic(node* root1, node* root2){    // if Both roots are null    // then tree is isomorphic    if (root1 == NULL and root2 == NULL)        return true;Â
    // check if one node is false    else if (root1 == NULL or root2 == NULL)        return false;Â
    queue<node *> q1, q2;Â
    // enqueue roots    q1.push(root1);    q2.push(root2);Â
    int level = 0;    int size;Â
    vector<int> v2;Â
    unordered_map<int, int> mp;Â
    while (!q1.empty() and !q2.empty()) {Â
        // check if no. of nodes are        // not same at a given level        if (q1.size() != q2.size())            return false;Â
        size = q1.size();Â
        level++;Â
        v2.clear();        mp.clear();Â
        while (size--) {Â
            node* temp1 = q1.front();            node* temp2 = q2.front();Â
            // dequeue the nodes            q1.pop();            q2.pop();Â
            // check if value            // exists in the map            if (mp.find(temp1->data) == mp.end())                mp[temp1->data] = 1;Â
            else                mp[temp1->data]++;Â
            v2.push_back(temp2->data);Â
            // enqueue the child nodes            if (temp1->left)                q1.push(temp1->left);Â
            if (temp1->right)                q1.push(temp1->right);Â
            if (temp2->left)                q2.push(temp2->left);Â
            if (temp2->right)                q2.push(temp2->right);        }Â
        // Iterate through each node at a level        // to check whether it exists or not.        for (auto i : v2) {Â
            if (mp.find(i) == mp.end())                return false;Â
            else {                mp[i]--;Â
                if (mp[i] < 0)                    return false;Â
                else if (mp[i] == 0)                    mp.erase(i);            }        }Â
        // check if the key remain        if (mp.size() != 0)            return false;    }    return true;}Â
/* function that allocates a new node with the given data and NULL left and right pointers. */node* newnode(int data){Â Â Â Â node* temp = new node;Â Â Â Â temp->data = data;Â Â Â Â temp->left = NULL;Â Â Â Â temp->right = NULL;Â
    return (temp);}Â
/* Driver program*/Â
int main(){    // create tree    struct node* n1 = newnode(1);    n1->left = newnode(2);    n1->right = newnode(3);    n1->left->left = newnode(4);    n1->left->right = newnode(5);    n1->right->left = newnode(6);    n1->left->right->left = newnode(7);    n1->left->right->right = newnode(8);Â
    struct node* n2 = newnode(1);    n2->left = newnode(3);    n2->right = newnode(2);    n2->right->left = newnode(4);    n2->right->right = newnode(5);    n2->left->right = newnode(6);    n2->right->right->left = newnode(8);    n2->right->right->right = newnode(7);Â
    if (isIsomorphic(n1, n2) == true)        cout << "Yes";    else        cout << "No";Â
    return 0;} |
Java
// Java program to find two// Binary Tree are Isomorphic or notimport java.util.ArrayList;import java.util.HashMap;import java.util.LinkedList;import java.util.Queue;Â
class GFG{Â
// A binary tree node has data, // pointer to left and right childrenstatic class Node {Â Â int data;Â Â Node left, right;Â Â public Node(int data) Â Â {Â Â Â Â this.data = data;Â Â Â Â this.left = this.right = null;Â Â }};Â
// Function to return if tree // are isomorphic or notstatic boolean isIsomorphic(Node root1,                             Node root2) {  // If Both roots are null  // then tree is isomorphic  if (root1 == null &&       root2 == null)    return true;Â
  // Check if one node   // is false  else if (root1 == null ||            root2 == null)    return false;Â
  Queue<Node> q1 =         new LinkedList<>(),               q2 = new LinkedList<>();Â
  // Enqueue roots  q1.add(root1);  q2.add(root2);Â
  int level = 0;  int size;Â
  ArrayList<Integer> v2 =             new ArrayList<>();  HashMap<Integer,           Integer> mp = new HashMap<>();Â
  while (!q1.isEmpty() &&          !q2.isEmpty())  {    // check if no. of nodes are    // not same at a given level    if (q1.size() != q2.size())      return false;Â
    size = q1.size();    level++;    v2.clear();    mp.clear();Â
    while (size-- > 0)     {      // Dequeue the nodes      Node temp1 = q1.poll();      Node temp2 = q2.poll();Â
      // Check if value      // exists in the map      if (!mp.containsKey(temp1.data))        mp.put(temp1.data, 1);      else        mp.put(temp1.data,                mp.get(temp1.data) + 1);Â
      v2.add(temp2.data);Â
      // Enqueue the child nodes      if (temp1.left != null)        q1.add(temp1.left);Â
      if (temp1.right != null)        q1.add(temp1.right);Â
      if (temp2.left != null)        q2.add(temp2.left);Â
      if (temp2.right != null)        q2.add(temp2.right);    }Â
    // Iterate through each node     // at a level to check whether     // it exists or not.    for (Integer i : v2)     {      if (!mp.containsKey(i))        return false;      else      {        mp.put(i, mp.get(i) - 1);Â
        if (mp.get(i) < 0)          return false;        else if (mp.get(i) == 0)          mp.remove(i);      }    }Â
    // Check if the key remain    if (mp.size() != 0)      return false;  }  return true;}Â
// Driver programpublic static void main(String[] args) {  // Create tree  Node n1 = new Node(1);  n1.left = new Node(2);  n1.right = new Node(3);  n1.left.left = new Node(4);  n1.left.right = new Node(5);  n1.right.left = new Node(6);  n1.left.right.left = new Node(7);  n1.left.right.right = new Node(8);Â
  Node n2 = new Node(1);  n2.left = new Node(3);  n2.right = new Node(2);  n2.right.left = new Node(4);  n2.right.right = new Node(5);  n2.left.right = new Node(6);  n2.right.right.left = new Node(8);  n2.right.right.right = new Node(7);Â
  if (isIsomorphic(n1, n2))    System.out.println("Yes");  else    System.out.println("No");}   }Â
// This code is contributed by sanjeev2552 |
Python3
# Python3 program to find two# Binary Tree are Isomorphic or notfrom collections import dequeÂ
class node:         def __init__(self, x):                 self.data = x        self.left = None        self.right = NoneÂ
# Function to return if tree are# isomorphic or notdef isIsomorphic(root1, root2):         # If Both roots are null    # then tree is isomorphic    if (root1 == None and root2 == None):        return TrueÂ
    # Check if one node is false    elif (root1 == None or root2 == None):        return FalseÂ
    q1 = deque()    q2 = deque()Â
    # enqueue roots    q1.append(root1)    q2.append(root2)Â
    level = 0    size = 0Â
    v1 = []    m2 = {}Â
    while (len(q1) > 0 and len(q2) > 0):                 # Check if no. of nodes are        # not same at a given level        if (len(q1) != len(q2)):            return FalseÂ
        size = len(q1)        level += 1Â
        v2 = []        mp = {}Â
        while (size):            temp1 = q1.popleft()            temp2 = q2.popleft()Â
            # Check if value            # exists in the map            mp[temp1.data] = mp.get(temp1.data, 0) + 1Â
            v2.append(temp2.data)Â
            # enqueue the child nodes            if (temp1.left):                q1.append(temp1.left)Â
            if (temp1.right):                q1.append(temp1.right)Â
            if (temp2.left):                q2.append(temp2.left)Â
            if (temp2.right):                q2.append(temp2.right)                             v1 = v2            m2 = mp            size -= 1Â
        # Iterate through each node at a level        # to check whether it exists or not.        for i in v1:Â
            if i in m2:                return True            else:                m2[i] -= 1Â
                if (m2[i] < 0):                    return False                elif (m2[i] == 0):                    del m2[i]Â
        # Check if the key remain        if (len(m2) == 0):            return False                 return TrueÂ
# Driver codeif __name__ == '__main__':         # Create tree    n1 = node(1)    n1.left = node(2)    n1.right = node(3)    n1.left.left = node(4)    n1.left.right = node(5)    n1.right.left = node(6)    n1.left.right.left = node(7)    n1.left.right.right = node(8)Â
    n2 = node(1)    n2.left = node(3)    n2.right = node(2)    n2.right.left = node(4)    n2.right.right = node(5)    n2.left.right = node(6)    n2.right.right.left = node(8)    n2.right.right.right = node(7)Â
    if (isIsomorphic(n1, n2) == True):        print("Yes")    else:        print("No")Â
# This code is contributed by mohit kumar 29 |
C#
// C# program to find two// Binary Tree are Isomorphic or notusing System;using System.Collections;using System.Collections.Generic;  class GFG{  // A binary tree node has data, // pointer to left and right childrenclass Node {  public int data;  public Node left, right;  public Node(int data)   {    this.data = data;    this.left = this.right = null;  }};  // Function to return if tree // are isomorphic or notstatic bool isIsomorphic(Node root1,                             Node root2) {  // If Both roots are null  // then tree is isomorphic  if (root1 == null &&       root2 == null)    return true;    // Check if one node   // is false  else if (root1 == null ||            root2 == null)    return false;    Queue q1 = new Queue();  Queue q2 = new Queue();    // roots  q1.Enqueue(root1);  q2.Enqueue(root2);    int level = 0;  int size;    ArrayList v2 = new ArrayList();  Dictionary<int,int> mp = new Dictionary<int,int>();    while (q1.Count != 0 && q2.Count != 0)  {    // check if no. of nodes are    // not same at a given level    if (q1.Count != q2.Count)      return false;      size = q1.Count;    level++;    v2.Clear();    mp.Clear();      while (size-- > 0)     {      // Dequeue the nodes      Node temp1 = (Node)q1.Dequeue();      Node temp2 = (Node)q2.Dequeue();        // Check if value      // exists in the map      if (!mp.ContainsKey(temp1.data))        mp[temp1.data] = 1;      else        mp[temp1.data]++;        v2.Add(temp2.data);        // the child nodes      if (temp1.left != null)        q1.Enqueue(temp1.left);        if (temp1.right != null)        q1.Enqueue(temp1.right);        if (temp2.left != null)        q2.Enqueue(temp2.left);        if (temp2.right != null)        q2.Enqueue(temp2.right);    }      // Iterate through each node     // at a level to check whether     // it exists or not.    foreach (int i in v2)     {      if (!mp.ContainsKey(i))        return false;      else      {        mp[i]--;          if (mp[i] < 0)          return false;        else if (mp[i] == 0)          mp.Remove(i);      }    }      // Check if the key remain    if (mp.Count != 0)      return false;  }  return true;}  // Driver programpublic static void Main(string[] args) {     // Create tree  Node n1 = new Node(1);  n1.left = new Node(2);  n1.right = new Node(3);  n1.left.left = new Node(4);  n1.left.right = new Node(5);  n1.right.left = new Node(6);  n1.left.right.left = new Node(7);  n1.left.right.right = new Node(8);    Node n2 = new Node(1);  n2.left = new Node(3);  n2.right = new Node(2);  n2.right.left = new Node(4);  n2.right.right = new Node(5);  n2.left.right = new Node(6);  n2.right.right.left = new Node(8);  n2.right.right.right = new Node(7);    if (isIsomorphic(n1, n2))    Console.WriteLine("Yes");  else    Console.WriteLine("No");}   }Â
// This code is contributed by rutvik_56 |
Javascript
<script>Â
// Javascript program to find two// Binary Tree are Isomorphic or notÂ
// A binary tree node has data,// pointer to left and right childrenclass Node{         // Utility function to    // create a new node    constructor(key)    {        this.data = key;        this.left = this.right = null;    }}Â
// Function to return if tree// are isomorphic or notfunction isIsomorphic(root1, root2){         // If Both roots are null    // then tree is isomorphic    if (root1 == null &&        root2 == null)        return true;         // Check if one node    // is false    else if (root1 == null ||             root2 == null)        return false;         let q1 =[],    q2 = [];         // Enqueue roots    q1.push(root1);    q2.push(root2);         let level = 0;    let size;         let v2 = [];    let mp = new Map();         while (q1.length != 0 &&           q2.length != 0)    {                 // Check if no. of nodes are        // not same at a given level        if (q1.length != q2.length)            return false;                     size = q1.length;        level++;        v2 = [];                 while (size-- > 0)        {                         // Dequeue the nodes            let temp1 = q1.shift();            let temp2 = q2.shift();                         // Check if value            // exists in the map            if (!mp.has(temp1.data))                mp.set(temp1.data, 1);            else                mp.set(temp1.data,                mp.get(temp1.data) + 1);                         v2.push(temp2.data);                         // Enqueue the child nodes            if (temp1.left != null)                q1.push(temp1.left);                         if (temp1.right != null)                q1.push(temp1.right);                         if (temp2.left != null)                q2.push(temp2.left);                         if (temp2.right != null)                q2.push(temp2.right);        }             // Iterate through each node        // at a level to check whether        // it exists or not.        for(let i = 0; i < v2.length; i++)        {            if (!mp.has(v2[i]))                return false;            else            {                mp.set(v2[i], mp.get(v2[i]) - 1);                                 if (mp.get(v2[i]) < 0)                    return false;                else if (mp.get(v2[i]) == 0)                    mp.delete(v2[i]);            }        }                 // Check if the key remain        if (mp.size != 0)            return false;    }    return true;}Â
// Driver codeÂ
// Create treelet n1 = new Node(1);n1.left = new Node(2);n1.right = new Node(3);n1.left.left = new Node(4);n1.left.right = new Node(5);n1.right.left = new Node(6);n1.left.right.left = new Node(7);n1.left.right.right = new Node(8);Â
let n2 = new Node(1);n2.left = new Node(3);n2.right = new Node(2);n2.right.left = new Node(4);n2.right.right = new Node(5);n2.left.right = new Node(6);n2.right.right.left = new Node(8);n2.right.right.right = new Node(7);Â
if (isIsomorphic(n1, n2))    document.write("Yes");else    document.write("No");Â
// This code is contributed by patel2127Â
</script> |
Yes
Â
Time Complexity:Â O(N)
Auxiliary Space: O(N)Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!




