Time Limit: 1000MS Memory limit: 65536K
题目描述
二叉排序树的定义是:或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树。 今天我们要判断两序列是否为同一二叉排序树
输入
输出
示例输入
2 123456789 987654321 432156789 0
示例输出
NO NO
提示
来源
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
struct node
{
char data;
struct node *lchild,*rchild;
};
struct node *create(struct node *root,char e)
{
if(root==NULL)
{
root=new node;
root->data=e;
root->lchild=root->rchild=NULL;
}
else
{
if(e>root->data)
root->rchild=create(root->rchild,e);
else root->lchild=create(root->lchild,e);
}
return root;
}
int i;
char xianxu(struct node *root,char c[])
{
if(root)
{
c[i++]=root->data; //此处字符数组是一个个赋值的,不会再末尾加上'\0'
xianxu(root->lchild,c);
xianxu(root->rchild,c);
}
return *c;
}
int main()
{
int n;
struct node *root,*p;
char a[100],b[1000],c[1000],d[1000];
while(cin>>n&&n!=0)
{
root=NULL; //要记得在每次建树的时候,从空树开始建。
cin>>a; //在输入字符数组时,会默认在最后加上'\0'
for(int i=0; i<strlen(a); i++)
root=create(root,a[i]);
i=0;
xianxu(root,c);
c[i]='\0';
while(n--)
{
p=NULL;
cin>>b;
for(int i=0; i<strlen(b); i++)
p=create(p,b[i]);
i=0;
xianxu(p,d);
d[i]='\0'; //字符数组要以'\0'为结束
if(strcmp(c,d)==0)
cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
}