7-28 搜索树判断 (25 分)

对于二叉搜索树,我们规定任一结点的左子树仅包含严格小于该结点的键值,而其右子树包含大于或等于该结点的键值。如果我们交换每个节点的左子树和右子树,得到的树叫做镜像二叉搜索树。

现在我们给出一个整数键值序列,请编写程序判断该序列是否为某棵二叉搜索树或某镜像二叉搜索树的前序遍历序列,如果是,则输出对应二叉树的后序遍历序列。

输入格式:
输入的第一行包含一个正整数N(≤1000),第二行包含N个整数,为给出的整数键值序列,数字间以空格分隔。

输出格式:
输出的第一行首先给出判断结果,如果输入的序列是某棵二叉搜索树或某镜像二叉搜索树的前序遍历序列,则输出YES,否侧输出NO。如果判断结果是YES,下一行输出对应二叉树的后序遍历序列。数字间以空格分隔,但行尾不能有多余的空格。

输入样例1:
7
8 6 5 7 10 8 11
输出样例1:
YES
5 7 6 8 11 10 8
输入样例2:
7
8 6 8 5 10 9 11
输出样例2:
NO

解答:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 1000

typedef struct T{
    struct T *Left;
    struct T *Right;
    int Data;
} BinTrees;

int FrontErgodicSequence[MAX]={0};
int InputSequence[MAX]={0};
int BehindErgodicSequence[MAX]={0};

int pos=0,n;

typedef BinTrees* BinTree;

BinTree Insert(BinTree root,int Data);
BinTree ImageInsert(BinTree root,int Data);
void FrontErgodic(BinTree root);
void BehindErgodic(BinTree root);
int JudgeIsError(BinTree root);

int main(){
    scanf("%d",&n);
    BinTree root=NULL,ImageRoot=NULL;
    for(int i=0; i<n; i++){
        scanf("%d",&InputSequence[i]);
        root=Insert(root,InputSequence[i]);
        ImageRoot=ImageInsert(ImageRoot,InputSequence[i]);
    }
    FrontErgodic(root);
    if(JudgeIsError(root)){
        pos=0;
        FrontErgodic(ImageRoot);
        if(JudgeIsError(ImageRoot)){
            printf("NO");
        }
    }
    return 0;
}

BinTree Insert(BinTree root,int Data){
    if(root==NULL){
        root=(BinTree)malloc(sizeof(BinTrees));
        root->Left=NULL;
        root->Right=NULL;
        root->Data=Data;
        return root;
    }
    BinTree TRoot=(BinTree)malloc(sizeof(BinTrees));
    TRoot=root;
    while(1){
        if(Data>=TRoot->Data){
            if(TRoot->Right!=NULL){
                TRoot=TRoot->Right;
            }else{
                TRoot->Right=(BinTree)malloc(sizeof(BinTrees));
                TRoot->Right->Right=NULL;
                TRoot->Right->Left=NULL;
                TRoot->Right->Data=Data;
                break;
            }
        }else{
            if(TRoot->Left!=NULL){
                TRoot=TRoot->Left;
            }else{
                TRoot->Left=(BinTree)malloc(sizeof(BinTrees));
                TRoot->Left->Right=NULL;
                TRoot->Left->Left=NULL;
                TRoot->Left->Data=Data;
                break;
            }
        }
    }
    return root;
}

BinTree ImageInsert(BinTree root,int Data){
    if(root==NULL){
        root=(BinTree)malloc(sizeof(BinTrees));
        root->Left=NULL;
        root->Right=NULL;
        root->Data=Data;
        return root;
    }
    BinTree TRoot=(BinTree)malloc(sizeof(BinTrees));
    TRoot=root;
    while(1){
        if(Data<TRoot->Data){
            if(TRoot->Right!=NULL){
                TRoot=TRoot->Right;
            }else{
                TRoot->Right=(BinTree)malloc(sizeof(BinTrees));
                TRoot->Right->Right=NULL;
                TRoot->Right->Left=NULL;
                TRoot->Right->Data=Data;
                break;
            }
        }else{
            if(TRoot->Left!=NULL){
                TRoot=TRoot->Left;
            }else{
                TRoot->Left=(BinTree)malloc(sizeof(BinTrees));
                TRoot->Left->Right=NULL;
                TRoot->Left->Left=NULL;
                TRoot->Left->Data=Data;
                break;
            }
        }
    }
    return root;
}

void FrontErgodic(BinTree root){
    if(root==NULL){
        return;
    }else{
        FrontErgodicSequence[pos++]=root->Data;
        FrontErgodic(root->Left);
        FrontErgodic(root->Right);
    }
}

void BehindErgodic(BinTree root){
    if(root==NULL){
        return;
    }else{
        BehindErgodic(root->Left);
        BehindErgodic(root->Right);
        BehindErgodicSequence[pos++]=root->Data;
    }
}

int JudgeIsError(BinTree root){
    int i;
    for(i=0; i<n; i++){
        if(FrontErgodicSequence[i]!=InputSequence[i]){
            break;
        }
    }
    if(i==n){
        printf("YES\n");
        pos=0;
        BehindErgodic(root);
        for(int j=0; j<n; j++){
            printf("%d",BehindErgodicSequence[j]);
            if(j!=n-1){
                printf(" ");
            }
        }
        return 0;
    }
    return 1;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值