1043. Is It a Binary Search Tree (25)

本文提供了一种解决PAT-A 1043题的方法,通过构造二叉排序树并进行先序和后序遍历来判断给定序列是否符合二叉排序树或其镜像树的特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目详情:https://www.patest.cn/contests/pat-a-practise/1043

提交:
这里写图片描述

提交代码:

#include <iostream>
#include <malloc.h>
#include <vector>
using namespace std;
#define N 1010
typedef struct BTree
{
    int data;
    struct BTree* leftChild;
    struct BTree* rightChild;
}BTree;
vector<int> post,postMirror;
int number[N],n;
int numberT[N],rear;
BTree* insertBST( BTree *root,int key ) //插入一个节点到二叉排序树 
{
    if( root == NULL )
    {
        root = (BTree*)malloc(sizeof(BTree));
        root->data = key;
        root->leftChild = root->rightChild = NULL;
    }
    else if( root->data > key )  //根节点中的值大,则到左子树中去 
    {
        root->leftChild = insertBST(root->leftChild,key);
    }
    else if( root->data <= key )
    {
        root->rightChild = insertBST(root->rightChild,key);
    }
    return root;
}
void preOrder( BTree *bt ) //对二叉排序树进行先序遍历 
{
    if( bt != NULL )
    {
        numberT[++rear] = bt->data;
        preOrder(bt->leftChild);
        preOrder(bt->rightChild);
    }
}
void preOrderMirror( BTree *bt )//对镜像树进行先序遍历 
{
    if( bt != NULL )
    {
        numberT[++rear] = bt->data;
        preOrderMirror( bt->rightChild );
        preOrderMirror( bt->leftChild );
    }
}
void postOrder( BTree *bt )//对二叉排序树进行后续遍历 
{
    if( bt != NULL )
    {
        postOrder(bt->leftChild);
        postOrder(bt->rightChild);
        post.push_back(bt->data);
    }
}
void postOrderMirror( BTree *bt ) //对镜像树进行后序遍历 
{
    if( bt != NULL )
    {
        postOrderMirror( bt->rightChild );
        postOrderMirror( bt->leftChild );
        postMirror.push_back(bt->data);
    }
}
bool compareArray( int array1[],int array2[] ) //比较两个数组是否相等 
{
    for( int i=0;i<n;++i )
    {
        if( array1[i] != array2[i] )
            return false;
    }
    return true;
}
int main()
{
    cin>>n;
    BTree* root = NULL;
    for( int i=0;i<n;++i )
    {
        cin>>number[i];
        root = insertBST(root,number[i]);
    }
    rear = -1;
    preOrder(root);
    bool result1 = compareArray(number,numberT);
    if( result1 )  
    {   //是二叉排序树 
        cout<<"YES"<<endl;
        postOrder(root);
        for( int i=0;i<post.size();++i )
        {
            if( i == post.size()-1 )
                cout<<post[i]<<endl;
            else
                cout<<post[i]<<" ";
        }
        return 0;
    }
    //如果不是,则检查是否为镜像树
    rear = -1;
    preOrderMirror(root);
    bool result2 = compareArray(number,numberT);
    if( result2 )
    {
        cout<<"YES"<<endl;
        postOrderMirror(root);
        for( int i=0;i<postMirror.size();++i )
        {
            if( i == postMirror.size()-1 )
                cout<<postMirror[i]<<endl;
            else
                cout<<postMirror[i]<<" ";
        }
        return 0;
    }
    else
        cout<<"NO"<<endl;   
    return 0;
}

不要问我为什么可以对镜像树的先序遍历,后序遍历可以写成这样,我也是看的别人说的算法思想,然后自己写出来的,具体的我也不太懂。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值