Problem Description
判断两序列是否为同一二叉搜索树序列
Input
开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束。
接下去一行是一个序列,序列长度小于10,包含(0~9)的数字,没有重复数字,根据这个序列可以构造出一颗二叉搜索树。
接下去的n行有n个序列,每个序列格式跟第一个序列一样,请判断这两个序列是否能组成同一颗二叉搜索树。
Output
如果序列相同则输出YES,否则输出NO
Sample Input
2
567432
543267
576342
0
Sample Output
YES
NO
<strong>//二叉搜索树、、、、、、</strong>
<strong>//给定序列,判断两个序列是否为同一颗二叉搜索树的序列;</strong>
<strong>//主要为操作是建树与遍历</strong><img alt="鄙视" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/despise.gif" style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);" />
<strong>
</strong>
<strong><a target=_blank href="http://acm.hdu.edu.cn/showproblem.php?pid=3791">任意门</a>进入原题:</strong>
#include<iostream>
#include<malloc.h>
#include<string>
using namespace std;
#define N 10+2
typedef struct node
{
char data;
struct node *lchild,*rchild;
}bitree;
bitree *root;
void creattree(bitree *tree,char c)
{
if(!root)
{
root=(bitree *)malloc(sizeof(bitree));
root->data=c;
root->lchild=NULL;
root->rchild=NULL;
}
else
{
if(c>tree->data)
{
if(!tree->rchild)
{
tree->rchild=(bitree *)malloc(sizeof(bitree));
tree->rchild->data=c;
tree->rchild->lchild=NULL;
tree->rchild->rchild=NULL;
}
else
{
creattree(tree->rchild,c);
}
}
else
{
if(!tree->lchild)
{
tree->lchild=(bitree *)malloc(sizeof(bitree));
tree->lchild->data=c;
tree->lchild->lchild=NULL;
tree->lchild->rchild=NULL;
}
else
{
creattree(tree->lchild,c);
}
}
}
}
int coun;
void preorder(bitree *h,string &s)
{
if(h)
{
s[coun++]=h->data;
//cout<<h->data<<" ";
preorder(h->lchild,s);
preorder(h->rchild,s);
}
}
void free_tree(bitree *h)
{
if(h)
{
free_tree(h->lchild);
free_tree(h->rchild);
delete h;
}
}
int main()
{
int n;
string s1,str;
while(cin>>n,n)
{
root=NULL;
cin>>str;
coun=0;
for(int i=0;i<str.size();i++)
{
creattree(root,str[i]);
}
preorder(root,str);
// for(int i=0;i<str.size();i++)
// cout<<str[i];
// cout<<endl;
while(n--)
{ root=NULL;
coun=0;
cin>>s1;
for(int i=0;i<s1.size();i++)
{
creattree(root,s1[i]);
}
preorder(root,s1);
if(str==s1)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
}//free_tree(root);
return 0;
}