Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6780797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:32:55+00:00 2026-05-26T16:32:55+00:00

1) Given 2 arrays containing elements of a complete Binary tree(level by level), without

  • 0

1) Given 2 arrays containing elements of a complete Binary tree(level by level), without actually reconstructing a tree(i.e. by only doing swaps in an array), how can I find whether those 2 arrays are isomorphic or not ?

2) A better solution if one isomorphic tree forms a Binary Search Tree.

update e.g.

     5 
    / \
    4  7
   /\  /\
  2  3 6 8

can be represented in array as 5 4 7 2 3 6 8

Isomorphic trees are trees which can be converted to one another by rotation about nodes

     5 
    / \
    4  7
   /\  /\
  2  3 6 8



     5 
    / \
    4  7
   /\  /\
  3  2 6 8



     5 
    / \
    4  7
   /\  /\
  3  2 8 6



     5 
    / \
    7  4
   /\  /\
  8  6 3 2
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:32:55+00:00Added an answer on May 26, 2026 at 4:32 pm

    For the first problem:

    A bit of notation:

    • t0, t1 – trees
    • value(t) – the number stored at node
    • left(t) – the left subtree
    • right(t) – the right subtree

    t1 and t2 are isomorphic, iff
    t1 and t2 are empty,

    or value (t1) == value (t2)

    and

    either left(t1) is isomorphic to left(t2) and right(t1) is isomorphic to right(t2),

    or left(t1) is isomorphic to right(t2) and right(t1) is isomorphic to left(t2)

    Assuming the trees are stored in an arrays, such that element 0 is the root and and if t is an index of an internal node 2t+1 and 2t+2 are indices of its immediate children, straightforward implementation:

    #include <stdio.h>
    
    #define N 7
    
    int a[] = { 5, 4, 7, 2, 3, 6, 8 };
    int b[] = { 5, 7, 4, 6, 8, 2, 3 };
    
    int
    is_isomorphic (int t1, int t2)
    {
      if (t1 >= N && t2 >= N)
        return 1;
    
      if (a [t1] != b [t2])
        return 0;
    
      return ((is_isomorphic (2*t1 + 1, 2*t2 + 1)
               && is_isomorphic (2*t1 + 2, 2*t2 + 2))
              || (is_isomorphic (2*t1 + 1, 2*t2 + 2)
                  && is_isomorphic (2*t1 + 2, 2*t2 + 1)));
    }
    
    int main ()
    {
      printf ("%s\n", (is_isomorphic (0, 0) ? "yes" : "no"));
      return 0;
    }
    

    For the second problem, at each step, we compare the subtree of a with the smaller root to the subtree of b with the smaller root and then the subtree of a with the bigger root to the subtree of b with the bigger root (smaller and bigger than the current roots of a and b).

    int
    is_isomorphic_bst (int t1, int t2)
    {
      if (t1 >= N && t2 >= N)
        return 1;
    
      if (a [t1] != b [t2])
        return 0;
    
      int t1l, t1r, t2l, t2r;
      if (a [2*t1 + 1] < a [t1] && a [t1] < a [2*t1 + 2])
        {
          t1l = 2*t1 + 1;
          t1r = 2*t1 + 2;
        }
      else if (a [2*t1 + 1] > a [t1] && a [t1] > a [2*t1 + 2])
        {
          t1l = 2*t1 + 2;
          t1r = 2*t1 + 1;
        }
      else
        return 0;
    
      if (b [2*t2 + 1] < b [t2] && b [t2] < b [2*t2 + 2])
        {
          t2l = 2*t2 + 1;
          t2r = 2*t2 + 2;
        }
      else if (b [2*t2 + 1] > b [t2] && b [t2] > b [2*t2 + 2])
        {
          t2l = 2*t2 + 2;
          t2r = 2*t2 + 1;
        }
      else
        return 0;
    
      return is_isomorphic_bst (t1l, t2l) && is_isomorphic_bst (t1r, t2r);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Saw this question recently: Given 2 arrays, the 2nd array containing some of the
Given an array containing other nested arrays, I want to create an array containing
Given two sorted arrays: A and B . The size of array A is
Duplicate In C arrays why is this true? a[5] == 5[a] Given an array
Given an array of 81 elements (meant to represent a 9x9 grid) how can
Consider this problem: You are given an array containing positive integers. All the integers
Given an array of elements in PHP, I wish to create a new two-dimensional
Given a list of arrays and lots of setup time I need to quickly
I have a string. I need to replace all instances of a given array
Given an array of n Objects, let's say it is an array of strings

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.