PAT A1043 Is It a Binary Search Tree (25 分)

本文介绍了一种算法,用于判断给定的先序遍历序列是否属于二叉搜索树(BST)或其镜像。通过构建BST并比较前序遍历序列,确定树的类型,并输出相应的后序遍历序列。

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

  • 题目:
    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

    The left subtree of a node contains only nodes with keys less than the node’s key.
    The right subtree of a node contains only nodes with keys greater than or equal to the node’s key.
    Both the left and right subtrees must also be binary search trees.

If we swap the left and right subtrees of every node, then the resulting tree is called the Mirror Image of a BST.

Now given a sequence of integer keys, you are supposed to tell if it is the preorder traversal sequence of a BST or the mirror image of a BST.
Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤1000). Then N integer keys are given in the next line. All the numbers in a line are separated by a space.
Output Specification:

For each test case, first print in a line YES if the sequence is the preorder traversal sequence of a BST or the mirror image of a BST, or NO if not. Then if the answer is YES, print in the next line the postorder traversal sequence of that tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

  • 题目大意
    给出树的先序遍历序列,判断这棵树是否为BST或者镜像BST(条件互换),若为BST或者镜像BST,则输出YES,以及后序序列,否则输出NO
  • 解题思路
    1.通过给出的序列构造BST
    2.前序遍历BST,若前序序列和给出的序列完全相同,则是BST,否则两两交换树节点的位置,得到镜像BST,若镜像BST的前序序列和给出的序列相同,则是BST
    3.若两次判断都为否,则不是BST,输出NO

代码实现:

//解题思路:将输入节点按照顺序构建BST,然后判断前序遍历和交换后的前序遍历是否和输入
//的相同,只要有一个序列相同,则为BST,否则不是
#include <cstdio>
#include <vector>
using namespace std;
typedef struct Node{
    Node* left, *right;
    int data;
}node;
void insert_bin(node* &root, int x){       //插入节点,由于要修改指针,所以一定要用&
    if(root == nullptr){     //如果根节点为空,则为插入位置
        root = new node;
        root->data = x;
        root->left = nullptr;
        root->right = nullptr;
    }else if(root->data <= x){
        insert_bin(root->right, x);
    }else if(root->data > x){
        insert_bin(root->left, x);
    }
}
node* creat_BST(vector<int> num){           //创建BST
    if(num.size() == 0)
        return nullptr;
    int size1 = num.size();
    node* root = nullptr;
    for(int i = 0; i < size1; i++){
        insert_bin(root, num[i]);
    }
    return root;
}
node* swap_BST(node* root){                       //自下而上交换节点的位置
    if(root == nullptr){
        return nullptr;
    }else{
        swap_BST(root->left);
        swap_BST(root->right);
        node* temp = root->right;
        root->right = root->left;
        root->left = temp;
    }
    return root;
}
//前序遍历
vector<int> temp;
void preorder(node* root){
    if(!root)
        return;
    temp.push_back(root->data);
    preorder(root->left);
    preorder(root->right);
}
//后续遍历BST
vector<int> ans;
void postorder(node* root){
    if(!root)
        return;
    else{
        postorder(root->left);
        postorder(root->right);
        ans.push_back(root->data);
    }
}
int main()
{
    //输入BST的信息
    int N;
    scanf("%d", &N);
    vector<int> num;
    for(int i = 0; i < N; i++){
        int temp1;
        scanf("%d", &temp1);
        num.push_back(temp1);
    }
    node* root = creat_BST(num);
    preorder(root);
    if(num == temp){
        printf("YES\n");
        postorder(root);
        for(int i = 0; i < ans.size(); i++){
            if(i != 0){
                printf(" %d", ans[i]);
            }else
                printf("%d", ans[0]);
        }
        return 0;
    }
    temp.clear();
    swap_BST(root);
    preorder(root);
    if(num == temp){
        printf("YES\n");
        postorder(root);
        for(int i = 0; i < ans.size(); i++){
            if(i != 0){
                printf(" %d", ans[i]);
            }else
                printf("%d", ans[0]);
        }
        return 0;
    }
    printf("NO");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值