2482 二叉排序树

二叉排序树

Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description

二叉排序树的定义是:或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树。 今天我们要判断两序列是否为同一二叉排序树
Input

开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束。
接下去一行是一个序列,序列长度小于10,包含(0~9)的数字,没有重复数字,根据这个序列可以构造出一颗二叉排序树。
接下去的n行有n个序列,每个序列格式跟第一个序列一样,请判断这两个序列是否能组成同一颗二叉排序树。(数据保证不会有空树)
Output

Sample Input

2
123456789
987654321
432156789
0
Sample Output

NO
NO
大体思路有两种:
一:把两个树建出来,然后写一个函数判断他们是不是一样的;
二:把两个树建出来,然后把他们的先序遍历写出来,看他们的先序遍历是不是一样;

方法一:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct node
{
    char data;
    struct node *l, *r;
};

int f;
char a[20], b[20];

struct node *creat(struct node *root, char x)
{
    if(root==NULL)
    {
        root=(struct node *)malloc(sizeof(struct node));//要存数的时候可别忘了给根开空间
        root->data=x;
        root->l=NULL;
        root->r=NULL;
    }
    else
    {
        if(x>root->data) root->r=creat(root->r, x);
        else root->l=creat(root->l, x);
    }
    return root;
}

void judge(struct node *root1, struct node *root2)
{
    if((!root1&&root2)||(root1&&!root2))//这一个题可以不写这个判断,但如果题上没有说明树可能为空就要写
    {
        f = 1;
        return;
    }
    if(root1&&root2)
    {
        if(root1->data!=root2->data)
        {
            f = 1;
            return;
        }
        judge(root1->l, root2->l);
        judge(root1->r, root2->r);
    }
}

int main()
{
    int n, len, i;
    while(~scanf("%d", &n)&&n)
    {
        struct node *root1 = NULL;
        getchar();
        scanf("%s", a);
        len=strlen(a);
        for(i=0; i<len; i++)
        {
            root1=creat(root1, a[i]);
        }
        while(n--)
        {
            f = 0;//一定要每一组比较的时候都要把f初始化为0;
            struct node *root2 = NULL;
            scanf("%s", b);
            for(i=0; i<len; i++)
            {
                root2=creat(root2, b[i]);
            }
            judge(root1, root2);
            if(f) printf("NO\n");//大小写不要输错呦
            else printf("YES\n");
        }
    }
    return 0;
}



/***************************************************
Result: Accepted
Take time: 0ms
Take Memory: 160KB
Submit time: 2019-07-31 18:48:25
****************************************************/

方法二:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct node
{
    int data;
    struct node *l, *r;
};
int a[20], b[20], f[20], g[20], p, q;
char c[20], d[20];
struct node *creat(struct node *root, int x)
{
    if(root==NULL)
    {
        root=(struct node *)malloc(sizeof(struct node));
        root->data=x;
        root->l=NULL;
        root->r=NULL;
    }
    else
    {
        if(x>root->data) root->r=creat(root->r, x);
        else root->l=creat(root->l, x);
    }
    return root;
}
void str(struct node *root)//存初始序列的先序遍历
{
    if(root)
    {
        a[p++]=root->data;
        str(root->l);
        str(root->r);
    }
}
void str1(struct node *root)
{
    if(root)
    {
        b[q++]=root->data;
        str1(root->l);//之前错的时候是把str1写成了str,要再仔细一点呀
        str1(root->r);
    }
}
int main()
{
    int n, i;
    while(~scanf("%d", &n)&&n)
    {
        struct node *root=NULL;
        getchar();//当前面是数字,后面是字符串的时候最好加上getchar;
        scanf("%s", c);
        int len=strlen(c);
        for(i=0; i<len; i++)
        {
            f[i]=c[i]-'0';//把字符类型转化为int类型
        }
        for(i=0; i<len; i++)
        {
            root=creat(root, f[i]);
        }
        p = 0;
        str(root);
        while(n--)
        {
            struct node *root1=NULL;
            scanf("%s", d);
            len = strlen(d);
            for(i=0; i<len; i++)
            {
                g[i]=d[i]-'0';
            }
            for(i=0; i<len; i++)
            {
                root1=creat(root1, g[i]);
            }
            q = 0;
            str1(root1);
            for(i=0; i<len; i++)
            {
                if(a[i]!=b[i]) break;
            }
            if(i==len) printf("YES\n");
            else printf("NO\n");
        }
    }
    return 0;
}


/***************************************************
Result: Accepted
Take time: 0ms
Take Memory: 156KB
Submit time: 2019-07-31 19:04:45
****************************************************/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值