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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:05:27+00:00 2026-05-29T16:05:27+00:00

Can anyone explain why I am getting the output twice here? # include<iostream> #

  • 0

Can anyone explain why I am getting the output twice here?

# include<iostream>
# include<conio.h>
# include <stdio.h>

using namespace std;

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

struct tree * insert(struct tree * root,int value)
{
   if (root==NULL)
   {
      struct tree * node= (struct tree *)malloc(sizeof(struct tree));
      node->data=value;
      node->right=NULL;
      node->left=NULL;
      return node;
   }
   else if(root->data > value )
      root->left=insert(root->left,value);
   else
      root->right=insert(root->right,value);
}

int same_tree(struct tree * root1, struct tree* root2)
{
   if((root1==NULL && root2!=NULL) || (root1!=NULL && root2==NULL))
   {
      cout << " tree are not equal \n"; 
      return -1;
   }
   if(root1 && root2)
   {
      same_tree(root1->left,root2->left);
      if(root1->data!=root2->data)
      {
         cout << "tree not equal \n";   
         return -1;                        
      } 
      same_tree(root1->right,root2->right);
   }   
}

int main()
{
   struct tree * root=NULL;
   root= insert(root,8);
   insert(root,6);
   insert(root,7);
   insert(root,5);
   insert(root,1);
   insert(root,20);
   insert(root,15);
   struct tree * root2=NULL; 
   root2= insert(root2,8);
   insert(root2,6);
   insert(root2,7);
   insert(root2,5);
   insert(root2,1);
   insert(root2,20);
   insert(root2,18);
   int j= same_tree(root,root2);
   if(j==-1)
      cout << " tree not eqqual \n";
   else
      cout << "tree are equal\n";
   getch();
   return 0;   
}

The program is written to compare if two tree are same ( in the same hierarchial structure and data they contain) . The two tree i am comparing here is passed from main (root and root2).
In case of equal tree i get the O/o as “tree are equal” once . But in case tree are not equal i get an o/p “tre not equal ” and in next line as “tree are equal ” . I am unable to decipher Why ? I have written down the entire program so that anyone can copy paste and run it on their system . I guess The problem lies somewhere in recursive call of same_tree but where and why is something i am not getting

  • 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-29T16:05:29+00:00Added an answer on May 29, 2026 at 4:05 pm

    When comparing the trees you only return -1 (error) if the left node differs. You just ignore the right nodes. Also, if your trees only differ on the right side your same_tree does not return a value at all. I’m actually surprised this compiled at all.

    If you look at your same_tree closely you will see that it checks if only one of the let/right is null and then if non are null. However, if they are both null you just fall through. You also ignore the return value at many points.

    Say you have two roots, each with value 5 and one of them has left 7 while the other has left 8. Now when entering the same_tree you will compare the lefts which will front that that are different. However, the return value is ignored and you will continue with comparing the root value (both 5) and since they are same you will not return the error code and your main method will think every thing is fine and dandy and print that they are equal.

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

Sidebar

Related Questions

#include iostream using namespace std; class A { public: void mprint() { cout<<\n TESTING
Can anyone explain why I am getting segmentation fault in the following example? #include
Can anyone explain why I'm getting this compile error? Duplicate 'Rad.Core.Aop.MethodArgumentValidation' attribute E:\Scripting\Rad.Core\Properties\AssemblyInfo.cs This
can anyone explain why I am getting the following results? Dim badDecimal As Decimal
Not really getting the point of the map function. Can anyone explain with examples
Can anyone explain what advantages there are to using a tool like MSBuild (or
Can anyone explain to me why im getting a http 400 error when trying
Can anyone explain me what this code does after getting NSArray.... - (UIViewController *)sampleForIndexPath:(NSIndexPath
Can anyone explain to me what is going on here? First off, I think
Can anyone explain to me why im getting a .exe has encountered a problem

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.