hnust 1578 二叉排序树

          思路就是先把标准树建立起来,然后循环n次,每次在建一棵树,和标准树对比就可以了,代码中有注释的哦!

#include <iostream>
#include<string.h>
using namespace std;
struct tree
{
    tree *l,*r;//左树和右树
    int data;//数据
};
tree* ru(tree *t,int x)//建树
{
    if(t==NULL)//根
    {
        t=new tree;
        t->l=NULL;
        t->r=NULL;
        t->data=x;
    }
    else
    {
        if(t->data>x)
        {
            t->l=ru(t->l,x);
        }
        else t->r=ru(t->r,x);
    }
    return t;
}
int show(tree *t1,tree *t2)//对比两棵树
{
    if(t1==NULL&&t2==NULL)return 1;
    if(t1&&t2)
    {
        if(t1->data!=t2->data)return 0;
        else if((show(t1->l,t2->l))&&show(t1->r,t2->r))return 1;
    }
    return 0;
}
int main()
{
    int n,i;
    char a[15];
    //这个建树和while里面的建树一样
    tree *tre1=new tree;
    tre1=NULL;
    cin>>a;
    int l=strlen(a);

    for(i=0; i<l; i++)
    {
        tre1=ru(tre1,a[i]-'0');
    }
    cin>>n;
    while(n--)
    {
        //建立查询的树
        cin>>a;
        l=strlen(a);
        tree *tre2=new tree;
        tre2=NULL;
        for(i=0; i<l; i++)
        {
            tre2=ru(tre2,a[i]-'0');
        }
        
        i=show(tre1,tre2);//对比两树是否相等
        if(i==1)cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值