Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Interview Problems on Tree
  • Practice Tree
  • MCQs on Tree
  • Tutorial on Tree
  • Types of Trees
  • Basic operations
  • Tree Traversal
  • Binary Tree
  • Complete Binary Tree
  • Ternary Tree
  • Binary Search Tree
  • Red-Black Tree
  • AVL Tree
  • Full Binary Tree
  • B-Tree
  • Advantages & Disadvantages
Open In App
Next Article:
Tree Isomorphism Problem
Next article icon

Tree Isomorphism Problem

Last Updated : 01 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

Given two Binary Trees, the task is to check whether they are isomorphic or not. Two trees are called isomorphic if one of them can be obtained from the other by a series of flips, i.e. by swapping left and right children of several nodes. Any number of nodes at any level can have their children swapped.

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. 

Table of Content

  • [Expected Approach - 1] Using Recursion - O(n) Time and O(n) Space
  • [Expected Approach - 2] Using Iteration - O(n) Time and O(n) Space

[Expected Approach - 1] 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.

Follow the steps to solve the problem:

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 child of n1 is isomorphic to the left child of n2, and the right child of n1 is isomorphic to the right child of n2.
    • The left child of n1 is isomorphic to the right child of n2, and the right child of n1 is isomorphic to the left child of n2.
  • This ensures that the trees are either structurally identical or have been "flipped" at some levels while still being isomorphic.

Below is the implementation of the above approach:

C++
// C++ code to check if two trees are  // isomorphic using recursion #include <bits/stdc++.h> 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     //       / \     //      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->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
// Java code to check if two trees are  // isomorphic using recursion import java.util.*;  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         //       / \         //      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.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
# Python code to check if two trees are  # isomorphic using recursion 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, trees are      # isomorphic by definition     if root1 is None and root2 is None:         return True      # Exactly one of the root1 and root2 is None,     # trees not isomorphic     if root1 is None or root2 is None:         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) 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     #       / \     #      7   8     root1 = Node(1)     root1.left = Node(2)     root1.right = Node(3)     root1.left.left = Node(4)     root1.left.right = Node(5)     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.left = 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#
// C# code to check if two trees are  // isomorphic using recursion 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         //       / \         //      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.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)) {             Console.WriteLine("True");         }          else {             Console.WriteLine("False");         }     } } 
JavaScript
// Javascript code to check if two trees are  // isomorphic using recursion 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 //       / \ //      7   8 const 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.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 const 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)) {     console.log("True"); }  else {     console.log("False"); } 

Output
False 

Time Complexity: O(n), because each node in both trees is visited once during the isomorphism check, where n is the total number of nodes in the trees.
Auxiliary Space: O(h), due to the recursion stack, where h is the height of the trees; in the worst case, this can be O(n) for skewed trees.

[Expected Approach - 2] Using Iteration - O(n) Time and O(n) Space

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.

Please refer to Iterative Approach to check if two Binary Trees are Isomorphic or not for implementation.


Next Article
Tree Isomorphism Problem

K

kartik
Improve
Article Tags :
  • Tree
  • DSA
  • Microsoft
  • Amazon
Practice Tags :
  • Amazon
  • Microsoft
  • Tree

Similar Reads

    Isomorphism in N-ary Trees
    Given two N-ary trees having M nodes each. Also, given their edges and their roots respectively. The task is to check if they are isomorphic trees or not. If both the trees are isomorphic then print "Yes" else print "No". Examples: Input: M = 9, Root Node of tree-1: 1, Root Node of tree-2: 3 Edges o
    12 min read
    Top 50 Tree Coding Problems for Interviews
    Here is the collection of the Top 50 list of frequently asked interview questions on Tree. Problems in this Article are divided into three Levels so that readers can practice according to the difficulty level step by step.Easy ProblemsHeight of Binary TreeDetermine if two trees are identicalMirror t
    2 min read
    The Great Tree-List Recursion Problem.
    Asked by Varun Bhatia. Question: Write a recursive function treeToList(Node root) that takes an ordered binary tree and rearranges the internal pointers to make a circular doubly linked list out of the tree nodes. The”previous” pointers should be stored in the “small” field and the “next” pointers s
    1 min read
    Double Tree
    Write a program that converts a given tree to its Double tree. To create Double tree of the given tree, create a new duplicate for each node, and insert the duplicate as the left child of the original node. So the tree... 2 / \ 1 3is changed to... 2 / \ 2 3 / / 1 3 / 1And the tree 1 / \ 2 3 / \ 4 5i
    15+ min read
    Binary Tree in Python
    Binary Tree is a non-linear and hierarchical data structure where each node has at most two children referred to as the left child and the right child. The topmost node in a binary tree is called the root, and the bottom-most nodes are called leaves.Introduction to Binary TreeRepresentation of Binar
    9 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences