Segmentation Fault
I made some changes in your code…
#include
#include<bits/stdc++.h>
using namespace std;
class node
{
public:
string data;
node* right;
node* left;
node(string d)
{
data=d;
right=NULL;
left=NULL;
}
};
node* buildtree()
{
string str;
cin>>str;
if(str==“false”)
{
return NULL;
}
else
{
string d;
cin>>d;
node* root=new node(d);
root->left=buildtree();
root->right=buildtree();
return root;
}
}
void Check_structure(node* root1,node* root2,int &n)
{
if(root1==NULL && root2 == NULL)
{
return;
}
if(root2==NULL || root1 == NULL)
{
n=-1;
return;
}
if(root1->data==root2->data)
{
Check_structure(root1->left,root2->left,n);
Check_structure(root1->right,root2->right,n);
}
else if(root1->data!=root2->data)
{
n=-1;
}
return;
}
int main()
{
string d;
cin>>d;
node* root1=new node(d);
root1->left=buildtree();
root1->right=buildtree();
cin>>d;
node* root2=new node(d);
root2->left=buildtree();
root2->right=buildtree();
int n=0;
Check_structure(root1,root2,n);
if(n==0)
{
cout<<"true"<<endl;
}
else
{
cout<<"false"<<endl;
}
return 0;
}
please have a look.
thanks
Your code is not executing
Probably TLE
This is the updated code with some minor changes (from your code) but still the code is showing segmentation fault upon execution.
you can find the code here: https://ide.codingblocks.com/s/206344
(NOTE: you can not write code for root data reading in buildtree() as this function is recursive and will be called many times.)
this code will pass all the test cases except test case 1. I checked test case 1 manually and our code run perfectly on this test case also , so its look like there test case 1 is wrong.
you can also cross check test case 1 which is
50 true 290 true 23 false false false true 78 true 66 false false false
10 true 20 true 40 false false true 50 false false true 30 true 60 false false true 73 false true 88 false false
you can go ahead with this solution.
thanks.