PAT判断搜索树

解题思路,题目给出一行数字判断是否为二叉搜索树或者二叉镜像树
那么我们可以根据这行数字创建二叉一个二叉搜索树和一个二叉镜像
树。然后求出它们的前序,判断是二叉搜索树还是二叉镜像树,因为
前序序列一定和给出的数字一样所以很好判断。判断之后,求出后序

的表达式,然后输出之就OK了


#include <iostream>
#include <cstring>
#include <cstdio>
#define MAX 1005
using namespace std;
struct  BitNode{
    BitNode *left;
    BitNode *right;
    int data;
};
int arr[MAX];
int preOrderImg[MAX];
int preOrder[MAX];
int postOrder[MAX];
int index_post;
//插入节点
void InsertTree(BitNode* &root, int data ,bool isImg){
    if(root == NULL){
        root = new BitNode();
        root->left  = NULL;
        root->right = NULL;
        root->data  = data;
        return ;
    }else{
        if(isImg){
            if(root->data <= data){
                InsertTree(root->left,  data, isImg);
            }else{
                InsertTree(root->right, data, isImg);
            }
        }else{
            if(root->data > data){
                InsertTree(root->left,  data, isImg);
            }else{
                InsertTree(root->right, data, isImg);
            }
        }
    }
}
void CreatTree(BitNode* &root, int n, bool isImag){
    for(int i = 0 ;i < n ;i++){
        InsertTree(root, arr[i] , isImag);
    }
}
//求出前序
void CreatPreOrder(BitNode* root, bool isImg){
    if(root == NULL){
        return;
    }
    if(isImg){
        preOrderImg[index_post++] = root->data;
    }else{
        preOrder[index_post++]    = root->data;
    }
    if(root->left){
        CreatPreOrder(root->left, isImg);
    }
    if(root->right){
        CreatPreOrder(root->right,isImg);
    }
}
//求出后序
void CreatPostOrder(BitNode* root ){
    if(root == NULL){
        return;
    }
    if(root->left){
        CreatPostOrder(root->left);
    }
    if(root->right){
        CreatPostOrder(root->right);
    }
    postOrder[index_post++] = root->data;
}
bool CheckNum(int a[] , int b[],int len){
    for(int i = 0 ;i < len ;i++){
        if(a[i] != b[i]){
            return false;
        }
    }
    return true;
}
void PrintTree(int len){
    for(int i = 0 ;i < len ;i++){
        if(i == len-1){
            cout<<postOrder[len-1]<<endl;
        }else{
            cout<<postOrder[i]<<" ";
        }
    }
}
int main(){
    int n;
    BitNode *root    = NULL;//二叉树的数据结构
    BitNode *rootImg = NULL;//二叉镜像树的数据结构
    cin>>n;
    for(int i = 0 ; i < n ;i++){
        cin>>arr[i];
    }
    //创建二叉树搜索树和二叉镜像树
    CreatTree(rootImg, n, true);
    CreatTree(root,    n, false);
    CreatPreOrder(root, false);
    //求出二叉搜索树的前序
    index_post = 0;
    //判断是否为二叉搜索树
    if(CheckNum(arr,preOrder, n)){
        cout<<"YES"<<endl;
        CreatPostOrder(root);//求出后序序列
        PrintTree(index_post);//打印
    }else{
        index_post = 0;
        CreatPreOrder(rootImg, true);
        if(CheckNum(preOrderImg, arr, n)){
            cout<<"YES"<<endl;
            index_post = 0;
            CreatPostOrder(rootImg);
            PrintTree(index_post);
        }else{
            cout<<"NO"<<endl;
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值