首页 > 其他 > 详细

HDU 3791 二叉搜索树

时间:2015-03-13 13:54:19      阅读:238      评论:0      收藏:0      [点我收藏+]

题意:给出两串数字,每一串数字都构成一颗二叉树,问这两颗二叉树是否为同一颗二叉树。

可以用样例来考虑

5 6 7 4 3 2 6

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

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

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

画出这一颗二叉树为

技术分享

技术分享
 1 #include<iostream>  
 2 #include<cstdio>  
 3 #include<cstring> 
 4 #include <cmath>   
 5 #include<algorithm>  
 6 using namespace std;
 7 
 8 typedef long long LL;
 9 char s[25],s1[25];
10 int tree[2025],tree1[2025];
11 
12 int main()
13 {
14     int t,i,j;
15     while(scanf("%d",&t)!=EOF&&t){
16         memset(tree,-1,sizeof(tree));
17         cin>>s;
18         for(i=0;i<strlen(s);i++){
19             int c=s[i]-0;
20             j=1;
21             while(tree[j]!=-1){
22                 if(c<=tree[j]) j=2*j;
23                 else j=2*j|1;
24             }
25             tree[j]=c;
26         }
27         
28         while(t--){
29             memset(tree1,-1,sizeof(tree1));
30             cin>>s1;
31            for(i=0;i<strlen(s1);i++){
32             int c=s1[i]-0;
33             j=1;
34             while(tree1[j]!=-1){
35                 if(c<=tree1[j]) j=2*j;
36                 else j=2*j|1;
37             }
38             tree1[j]=c;
39         }
40         
41         for(i=1;i<=2005&&tree[i]==tree1[i];i++);//注意这里判断i的时候要比开的tree数组的大小小一些,因为这个wrongwrongwrong 
42         if(i>2005) printf("YES\n");
43         else printf("NO\n");
44         }
45     }
46     return 0;
47 }
View Code

 

HDU 3791 二叉搜索树

原文:http://www.cnblogs.com/wuyuewoniu/p/4334780.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!