您的位置:首页 > 其它

HDU 3791 二叉搜索树

2015-03-13 12:37 211 查看
题意:给出两串数字,每一串数字都构成一颗二叉树,问这两颗二叉树是否为同一颗二叉树。

可以用样例来考虑

5 6 7 4 3 2 6

6大于5,6是5的右儿子

7大于5,大于6,所以是5的右儿子的右儿子,即为6的右儿子

4小于5,所以4是5的左儿子

画出这一颗二叉树为

#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<algorithm>
using namespace std;

typedef long long LL;
char s[25],s1[25];
int tree[2025],tree1[2025];

int main()
{
int t,i,j;
while(scanf("%d",&t)!=EOF&&t){
memset(tree,-1,sizeof(tree));
cin>>s;
for(i=0;i<strlen(s);i++){
int c=s[i]-'0';
j=1;
while(tree[j]!=-1){
if(c<=tree[j]) j=2*j;
else j=2*j|1;
}
tree[j]=c;
}

while(t--){
memset(tree1,-1,sizeof(tree1));
cin>>s1;
for(i=0;i<strlen(s1);i++){
int c=s1[i]-'0';
j=1;
while(tree1[j]!=-1){
if(c<=tree1[j]) j=2*j;
else j=2*j|1;
}
tree1[j]=c;
}

for(i=1;i<=2005&&tree[i]==tree1[i];i++);//注意这里判断i的时候要比开的tree数组的大小小一些,因为这个wrongwrongwrong
if(i>2005) printf("YES\n");
else printf("NO\n");
}
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: