HDU - 3791 :二叉搜索树

///
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define per(i,a,b) for(int i=a;i<b;i++)
#define rep(i,b,a) for(int i=b;i>a;i--)
using namespace std;
struct node{
    int key;
    node *l,*r,*p;
};
int n;
node *NIL;
node *root;
int des[11],st[11];
int sum=0;
void insert1(int k){
    node *x=root;
    node *y=NIL;
    node *z;
    z=(node*)malloc(sizeof(node));
    z->key=k;
    z->l=NIL;
    z->r=NIL;
    z->p=NIL;
    while(x!=NIL){
        y=x;
        if(z->key<x->key){
            x=x->l;
        }
        else{
            x=x->r;
        }
    }
    z->p=y;
    if(y==NIL) root=z;
    else {
        if(z->key<y->key){
            y->l=z;
        }
        else y->r=z;
    }
}
void preorder0(node *root){
    if(root==NIL) return;
    des[sum++]=root->key;
    preorder0(root->l);
    preorder0(root->r);
}
void preorder1(node *root){
    if(root==NIL) return;
    st[sum++]=root->key;
    preorder1(root->l);
    preorder1(root->r);
}
bool Is_Same(){
    per(i,0,sum){
        if(des[i]!=st[i])return false;
    }
    return true;
}
int main(){
 ios::sync_with_stdio(false);
    while((cin>>n)&&n){
        char ch0[11];
         memset(des,0,sizeof(des));
       for(int i=0;i<=n;i++){
            sum=0;
            memset(st,0,sizeof(st));
            memset(ch0,0,sizeof(ch0));
            cin>>ch0;
            for(int j=0;ch0[j]!='\0';j++){
                int ch=ch0[j]-'0';
                insert1(ch);
            }
            if(i==0){
            preorder0(root);
            }
            else{
                preorder1(root);
                if(Is_Same()){
                    cout<<"YES"<<endl;
                }
                else {
                    cout<<"NO"<<endl;
                }
            }
            root=NIL;
        }
    }
return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值