题目1009:二叉搜索树
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:5733
解决:2538
- 题目描述:
-
判断两序列是否为同一二叉搜索树序列
- 输入:
-
开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束。
接下去一行是一个序列,序列长度小于10,包含(0~9)的数字,没有重复数字,根据这个序列可以构造出一颗二叉搜索树。
接下去的n行有n个序列,每个序列格式跟第一个序列一样,请判断这两个序列是否能组成同一颗二叉搜索树。
- 输出:
-
如果序列相同则输出YES,否则输出NO
- 样例输入:
-
2
567432
543267
576342
0
- 样例输出:
-
YES
NO
- 来源:
- 2010年浙江大学计算机及软件工程研究生机试真题
- 静态链表,建树对比。
-
#include <cstdio>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstring>
#include<vector>
using namespace std;
char tree[],t[];
int main(){
//freopen("D://INPUT.txt","r",stdin);
int n;
//cout<<n<<endl;
string s;
while(cin>>n&&n){
cin>>s;
//build(tree,s);
memset(tree,'-',sizeof(tree));
int i=;
int p;
while(i<s.length()){
p=;
while(tree[p]!='-'){
if(tree[p]>s[i]){
p=p*;
}
else{
if(tree[p]<s[i]){
p=p*+;
}
}
}
tree[p]=s[i++];
}
while(n--){
cin>>s;
memset(t,'-',sizeof(t));
i=;
while(i<s.length()){
p=;
while(t[p]!='-'){
if(t[p]>s[i]){
p=p*;
}
else{
if(t[p]<s[i]){
p=p*+;
}
}
}
t[p]=s[i++];
}
if(!strcmp(t,tree)){
cout<<"YES"<<endl;
}
else{
cout<<"NO"<<endl;
}
}
}
return ;
}