Question B: binary search tree

Title Description
Determine whether two sequences are the same binary search tree sequence

input
The start number n, (1 < = n < = 20) indicates that there are n numbers to judge, and the input ends when n= 0.
The next line is a sequence. The sequence length is less than 10, including (0 ~ 9) numbers, without repeated numbers. According to this sequence, a binary search tree can be constructed.
The next N lines have n sequences. Each sequence format is the same as the first sequence. Please judge whether these two sequences can form the same binary search tree.

output
Output YES if the sequence is the same, otherwise output NO

Sample input Copy
6
45021
12045
54120
45021
45012
21054
50412
0
Sample output Copy
NO
NO
YES
NO
NO
NO

```#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int flag;
struct node
{
int data;
struct node *l;
struct node *r;
};
struct node *creat(int n, struct node *root)
{
if(root==NULL)
{
root=(struct node*)malloc(sizeof(struct node));
root->data=n;
root->l=NULL;
root->r=NULL;
}
else
{
if(n<root->data)
root->l=creat(n,root->l);
else root->r=creat(n,root->r);
}
return root;
};
void compare(struct node *root1,struct node *root2)
{
if(root1!=NULL&&root2!=NULL)
{
if(root1->data!=root2->data)
{
flag=0;
return;
}
else
{
compare(root1->l,root2->l);
compare(root1->r,root2->r);
}
}
else if((root1==NULL&&root2!=NULL)||(root1!=NULL&&root2==NULL))
{
flag=0;
return;
}
}
int main()
{
int n,i,j,l;
struct node *root1,*root2;
while(scanf("%d",&n)!=EOF&&n)
{
char a[11],b[11];
scanf("%s",a);
root1=NULL;
l=strlen(a);
for(i=0; i<l; i++)
{
root1=creat(a[i]-'0',root1);
}
for(i=0; i<n; i++)
{
root2=NULL;
flag=1;
scanf("%s",b);
l=strlen(b);
for(j=0; j<l; j++)
{
root2=creat(b[j]-'0',root2);
}
compare(root1,root2);
if(flag==1)printf("YES\n");
else printf("NO\n");
}
}
return 0;
}

```

At first, I didn't find any mistakes in writing like this. Later, I found that a small place was ignored...

```#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int flag;
struct node
{
char data;
struct node *l;
struct node *r;
};
struct node *creat(int n, struct node *root)
{
if(root==NULL)
{
root=(struct node*)malloc(sizeof(struct node));
root->data=n+'0';//Convert to char type here
root->l=NULL;
root->r=NULL;
}
else
{
if(n+'0'<root->data)//Here also need to transform!!!
root->l=creat(n,root->l);
else root->r=creat(n,root->r);
}
return root;
};
void compare(struct node *root1,struct node *root2)
{
if(root1!=NULL&&root2!=NULL)
{
if(root1->data!=root2->data)
{
flag=0;
return;
}
else
{
compare(root1->l,root2->l);
compare(root1->r,root2->r);
}
}
else if((root1==NULL&&root2!=NULL)||(root1!=NULL&&root2==NULL))
{
flag=0;
return;
}
}
int main()
{
int n,i,j,l;
struct node *root1,*root2;
while(scanf("%d",&n)!=EOF&&n)
{
char a[11],b[11];
scanf("%s",a);
root1=NULL;
l=strlen(a);
for(i=0; i<l; i++)
{
root1=creat(a[i]-'0',root1);
}
for(i=0; i<n; i++)
{
root2=NULL;
flag=1;
scanf("%s",b);
l=strlen(b);
for(j=0; j<l; j++)
{
root2=creat(b[j]-'0',root2);
}
compare(root1,root2);
if(flag==1)printf("YES\n");
else printf("NO\n");
}
}
return 0;
}

```
Published 316 original articles, won praise and 10000 visitors+

Tags: less

Posted on Wed, 12 Feb 2020 11:07:47 -0500 by mhouldridge