• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Binary Tree Isomorphic to each other

March 22, 2017 by Dhaval Dave

Find weather two given trees are isomorphic to each other or not.
Lets understand What is isomorphic strings

AAB and XXY are IsoMorphic to each other, consider A replaces/morphs as X and B as Y, then both strings are isomorphic.
ABC and XXY are not IsoMorphic.isomorohic tree

In case of Trees Two binary trees A and B are isomorphic if they have the same shape means the values stored in the nodes do not affect whether two trees are isomorphic. Or its even called Quasi Isomorphic tree
(Mathematically we are discussing IsoMorphic rooted tree and not IsoMorphic Binary Tree, See at  : https://www.slideshare.net/AnasAssayuti/nota-math-discrete-tree Slide 28 onwards

Write a method isIsomorphic that returns true if its two tree are isomorphic and false otherwise.

Here w’ll discuss both ways to find if a tree is isomorphic to each other or not, One is with data and one is without data match

  1.  If ‘A’ Tree is structure wise mirror of other tree ‘B’ ( without its data values)
  2.  If ‘A’ Tree is exactly mirror of other tree ‘B’ (with of its data values

Algorithm for 1.

  • If any of the tree is null
    • Trees are not quasi isomorphic & return false
  • Check both trees tree1 and tree2
    • if tree1 and tree2 are null, traversal completed successfully.
      return true
  • Traverse Left subtree of tree1 and left subtree of tree2
  • Traverse Right subtree of tree1 and right subtree of tree2
    • If structures are identical, then return true
    • If trees are not identical, let us check one tree (or subtree) are mirror of other tree (subtree)
  • Traverse Left subtree of tree1 and right subtree of tree2
  • Traverse Right subtree of tree1 and left subtree of tree2
    • If structures are mirror structure, then return true
    • Else, return false

Sudo Code for 1&2.

bool isomorphic(struct treenode *treeone, struct treenode *treetwo)
{
    //if both tree null
    if (!treeone && !treetwo)
        return true;  
    // if one is null
    if((!treeone  ||  !treetwo))
        return false;
    // in case you want to check Data (case 2)
    if(treeone-data != treetwo->data)
        return false;
    return (
         (isomorphic(treeone->left, treetwo->left) 
          && isomorphic(treeone->right, treetwo->right)) || 
         (isomorphic(treeone->left, treetwo->right) 
          && isomorphic(treeone->right, treetwo->left))
         );
}

Working Code

// A C++ program to check if two given trees are isomorphic by gohired.in
#include <iostream>
using namespace std;

struct node
{
	int data;
	struct node* left;
	struct node* right;
};

bool isIsomorphic(node *treeone, node *treetwo)
{
    if (!treeone && !treetwo)
        return true;  
    if((!treeone  ||  !treetwo))
        return false;
   
        return (
                isIsomorphic(treeone->left, treetwo->right) 
                && isIsomorphic(treeone->right, treetwo->left)
                ||
                isIsomorphic(treeone->left, treetwo->left) 
                && isIsomorphic(treeone->right, treetwo->right) 
            );
}

node* newNode(int data)
{
	node* temp = new node;
	temp->data = data;
	temp->left = NULL;
	temp->right = NULL;

	return (temp);
}
int main()
{
	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(9);

	if (isIsomorphic(n1, n2) == true)
	cout << "Yes";
	else
	cout << "No";

	return 0;
}

Similar Articles

Filed Under: Amazon Interview Question, Interview Questions, Microsoft Interview Questions, problem Tagged With: Binary Tree, tree

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Python Dictionaries

Check if an array has duplicate numbers in O(n) time and O(1) space

Reversal of LinkedList

Fibonacci Hashing & Fastest Hashtable

FizzBuzz Solution C C++

Maximum difference between two elements s.t larger element appears after the smaller number

Find min element in Sorted Rotated Array (Without Duplicates)

K’th Largest Element in BST when modification to BST is not allowed

Spanning Tree

SAP Off Campus Hiring_ March 2015 Verbal Skills

Implement a generic binary search algorithm for Integer Double String etc

Maximum path sum between two leaves

Given Set of words or A String find whether chain is possible from these words or not

Binary Tree in Java

ADOBE Aptitude C Language Test

TicTacToe Game As Asked in Flipkart

Flipkart SDET Interview Experience

Maximum size of square sub matrix with all 1’s in a binary matrix

CodeChef Code SGARDEN

SAP Off Campus Hiring_ March 2015 Sample Questions

Subset Sum Problem Dynamic programming

Maximum occurred Smallest integer in n ranges

write a c program that given a set a of n numbers and another number x determines whether or not there exist two elements in s whose sum is exactly x

Inorder and Preorder traversals of a Binary Tree given. Output the Postorder traversal of it.

LeetCode: Container With Most Water

Generate next palindrome number

Hackerearth : Counting Subarrays

N teams are participating. each team plays twice with all other teams. Some of them will go to the semi final. Find Minimum and Maximum number of matches that a team has to win to qualify for finals ?

Longest Increasing Subsequence

Printing Longest Common Subsequence

Copyright © 2025 · Genesis Framework · WordPress · Log in