SDUTOJ 2482 二叉排序树

二叉排序树
Time Limit: 1000 ms Memory Limit: 65536 KiB

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
Hint

Source

思路:用最初的序列建立排序二叉树,再分别用两个数组记录其先序和后序遍历序列。将这三个序列分别与之后给出的序列对比

代码一:

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

char p[50],q[50],m[50];
int s=0,r=0,t=0;
struct node
{
    int date;
    struct node *lchild;
    struct node *rchild;
};
struct node *creatBST(struct node *root,char s);
void preorder(struct node *root);
void postorder(struct node *root);
int main()
{
    struct node *root;
    char a[50],b[50];
    int len,i,n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        root=NULL;
        scanf("%s",a);
        len=strlen(a);
        for(i=0;i<len;i++)
            root=creatBST(root,a[i]);
        preorder(root);
        p[s]='\0'; //注意末尾要加‘\0’将其变为字符串
        postorder(root);
        q[r]='\0';
        while(n--)
        {
            scanf("%s",b);
            if(strcmp(a,b)==0)
                printf("YES\n");
            else if(strcmp(p,b)==0)
                printf("YES\n");
            else if(strcmp(q,b)==0)
                printf("YES\n");
            else
                printf("NO\n");
        }
    }
    return 0;
}
struct node *creatBST(struct node *root,char s) //建立排序二叉树
{
    if(root==NULL)
    {
        root=(struct node*)malloc(sizeof(struct node));
        root->date=s;
        root->lchild=NULL;
        root->rchild=NULL;
    }
    else
    {
        if(s<root->date)
            root->lchild=creatBST(root->lchild,s);
        else
            root->rchild=creatBST(root->rchild,s);
    }
    return root;
}
void preorder(struct node *root)
{
    if(root!=NULL)
    {
        p[s++]=root->date;
        preorder(root->lchild);
        preorder(root->rchild);
    }
}
void postorder(struct node *root)
{
    if(root!=NULL)
    {
        postorder(root->lchild);
        postorder(root->rchild);
        q[r++]=root->date;
    }
}

思路:用最初的序列和之后给的序列分别建立排序二叉树。在分别进行先序遍历(不能是中序)用两个数组将其先序遍历序列记录,最后进行对比

代码二:

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

char p[20],q[20];
int x,y;
struct node
{
    int date;
    struct node *lchild;
    struct node *rchild;
};
struct node *creatBST(struct node *root,char s);
void preorder1(struct node *root);
void preorder2(struct node *root);
int main()
{
    struct node *root1,*root2;
    char a[20],b[20];
    int len1,len2,i,n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        x=0;
        root1=NULL;
        scanf("%s",a);
        len1=strlen(a);
        for(i=0;i<len1;i++)
            root1=creatBST(root1,a[i]);
        preorder1(root1);
        p[x]='\0'; //注意在数组末尾加‘\0’,将其变为字符串
        while(n--)
        {
            y=0;
            root2=NULL;
            scanf("%s",b);
            len2=strlen(b);
            for(i=0;i<len2;i++)
                root2=creatBST(root2,b[i]);
            preorder2(root2);
            q[y]='\0';
            if(strcmp(p,q)==0)
                    printf("YES\n");
            else
                printf("NO\n");
        }
    }
    return 0;
}
struct node *creatBST(struct node *root,char s)
{
    if(root==NULL)
    {
        root=(struct node*)malloc(sizeof(struct node));
        root->date=s;
        root->lchild=NULL;
        root->rchild=NULL;
    }
    else
    {
        if(s<root->date)
            root->lchild=creatBST(root->lchild,s);
        else
            root->rchild=creatBST(root->rchild,s);
    }
    return root;
}
void preorder1(struct node *root)
{
    if(root!=NULL)
    {
        p[x++]=root->date;
        preorder1(root->lchild);
        preorder1(root->rchild);
    }
}
void preorder2(struct node *root)
{
    if(root!=NULL)
    {
        q[y++]=root->date;
        preorder2(root->lchild);
        preorder2(root->rchild);
    }
}

猜你喜欢

转载自blog.csdn.net/Dmenghu/article/details/81531879
今日推荐