# ACM 数据结构-二叉树 HDU 3791二叉搜索树

<div class="panel_title" align="left">Problem Description</div><div class="panel_content">判断两序列是否为同一二叉搜索树序列</div><div class="panel_bottom"> </div>
<div class="panel_title" align="left">Input</div><div class="panel_content">开始一个数n，(1<=n<=20) 表示有n个需要判断，n= 0 的时候输入结束。

<div class="panel_title" align="left">Output</div><div class="panel_content">如果序列相同则输出YES，否则输出NO
</div><div class="panel_bottom"> </div>
<div class="panel_title" align="left">Sample Input</div><div class="panel_content"><pre><div style="FONT-FAMILY: Courier New,Courier,monospace">2
567432
543267
576342
0</div>

Sample Output
YES NO

Source

#include <iostream>
#include <cstring>
#include <cstdio>
#include <stdlib.h>
using namespace std;

int a[30],b[30],cnt = 0;

typedef struct tree
{
tree *l,*r;
int num;
} tree;
tree *root;
tree *creat(int x)
{
tree *t = (tree*)malloc(sizeof(tree));
t->l = 0;
t->r = 0;
t->num = x;
return t;
}

tree *inster(tree *s,int x)
{
tree *t;
if(s == NULL)
{
t = creat(x);
s = t;
}
else
{
if(x <= s->num)
s->l = inster(s->l,x);
else
s->r = inster(s->r,x);
}
return s;
}

void libian(tree *root)
{
if(root!=NULL)
{
b[cnt++] = root->num;
libian(root->l);
libian(root->r);
}
}

int main()
{
int n;
while(cin >> n,n)
{
cnt = 0;
root = NULL;
int tem;
char str[30];
cin >> str;
int len = strlen(str),i;
for(i = 0; i<len; i++)
{
tem = str[i] - '0';
root = inster(root,tem);
}
libian(root);
for(i = 0; i<len; i++)
a[i] = b[i];
while(n--)
{
cnt = 0;
cin >> str;
root = NULL;
for(i = 0; i<len; i++)
{
tem = str[i] - '0';
root = inster(root,tem);
}
libian(root);
for(i = 0; i<len; i++)
if(a[i]!=b[i])
{
cout << "NO" << endl;
break;
}
if(i>=len)
cout << "YES" << endl;
}
}
return 0;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：ACM 数据结构-二叉树 HDU 3791二叉搜索树 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)