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 7163551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:55:19+00:00 2026-05-28T13:55:19+00:00

I need to check two binary trees to see if they are similar… meaning

  • 0

I need to check two binary trees to see if they are similar… meaning if they have the exact same structure and then the data on the same levels (but the data doesn’t need to be in the same structure)…

Example

     A               A
  |     |         |     |
  B     C         C     B
 | |   | |       | |   | |
 D E   F G       G D   E F

The left binary tree is similar to the right (obviously there are MANY variations!)

Here’s the psuedocode I have thus far:

if( tree1 == NULL && tree2 == NULL ) return TRUE;

if( tree1 == NULL || tree2 == NULL ) return FALSE;

if( tree1->label != tree2->label ) return FALSE;

return (   TRUE
        && (  (   similarTrees(tree1->left, tree2->left)
               && similarTrees(tree1->right, tree2->right))
            || (   similarTrees(tree1->left, tree2->right)
                && similarTrees(tree1->right, tree2->left))  ));

My reasoning was that since the two left sub trees could be equal and the two right sub tree could be equal OR the left and right of one and the right and left of the other could be equal, then this logical recursion should work… it does not, however.

similarTree is the recursive function

Thanks for all your help!

  • 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-28T13:55:19+00:00Added an answer on May 28, 2026 at 1:55 pm

    The code and the example does not match.

    Code assumes that trees are equal if each subtree contains the same root label and the same children, albeit in an arbitrary order.

    The example, on the other hand, does not hold true for this. For example in the left tree, the children of C are F and G, whereas in the right G and D.

    To write code to check if these are similar, you must first clearly define what it means to be similar. E.g. should a label exists in both trees. Should it exists at the same level? Can it occur more than once?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to check equality between two MethodInfos. They are actually the exact same
I have two arrays. I need to check and see if the elements of
Hello I need to check if two lists have same elements in same order
To check that two variables have the same structure type I use a macro
I have two checkboxes. I need to check that at least one of them
I have one table and I need to check if two users, for whom
I have two NSStrings named country and searchtext. I need to check whether the
I have two fields representing data range (from and to). I need to check
I have a shell script in which I need to check whether two files
Hi I need to check the value of two variables if they are null

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.