(二叉排序树)HDU 3791 二叉搜索树

查看原题

题意

如题

思路

为每一行建立一个二叉排序树,进行对比

代码

#include<iostream>
#include <string.h>
using namespace std;
int tree1[10000];
int tree2[10000];
void insert(char node,int tree[]){
    int step=1;int nodedata=node-'0';
    while(tree[step]!=-1){
        if(tree[step]<nodedata)
            step=step*2+1;
        else
            step=step*2;
    }
    tree[step]=nodedata;
}
void build(char str[],int tree[]){
    int length=strlen(str);
    tree[1]=str[0]-'0';
    for(int i=1;i<length;i++){
        insert(str[i],tree);
    }
}
int main(){
    int n;
    while(cin>>n&&n!=0){
        memset(tree1,-1,sizeof(tree1));
        char str[10];
        cin>>str;
        build(str,tree1);
        while(n--){
            memset(tree2,-1,sizeof(tree2));
            cin>>str;
            build(str,tree2);
            int flag=0;
            for(int i=0;i<10000;i++){
                if(tree1[i]!=tree2[i])
                    flag=1;
            }
            if(flag==0)cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }

    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值