PTA甲级 1043 Is It a Binary Search Tree (C++)

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 ) N (≤1000) N(1000). Then N N 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.

Sample Input 1:

7
8 6 5 7 10 8 11

Sample Output 1:

YES
5 7 6 8 11 10 8

Sample Input 2:

7
8 10 11 8 6 7 5

Sample Output 2:

YES
11 8 10 7 5 6 8

Sample Input 3:

7
8 6 8 5 10 9 11

Sample Output 3:

NO

Caution:

思考的时候发现了一个特别有意思的性质,这道题可以利用这个性质很简单的做出来:
根据一个二叉查找树的前序遍历再创建一棵二叉查找树,创建的新树和老树是一样的。

证明也比较简单——首先前序遍历的第一个数字肯定是树的根结点,之后第一个小于(如果是“Mirrored”的树的话那就是第一个大于等于)根结点的数字一定是根结点的左孩子,也就是根结点左子树的根结点,同理,第一个大于等于(如果是“Mirrored”的话就是第一个小于)根结点的数字一定是根结点的右孩子,也就是根结点右子树的根结点,依次递归。

从另一个角度来说,一个普通的二叉树只根据前序遍历是无法唯一确定的,但是二叉查找树有了前序遍历其实就相当于同时有了中序遍历(根据与根结点的大小关系来确定左右子树),所以树是唯一的(另外这个题既然出了让你根据前序来写后序肯定也是因为只根据前序就能唯一确定这棵树啦)。

那这道题就简单很多了:根据前序遍历的顺序来建立一棵二叉查找树,然后对这棵树进行前序遍历,遍历出来的顺序和原数组对比,如果一致的话就再对其进行后续遍历并输出。不过这道题稍微复杂一丢丢的地方就在于需要判断两次(因为二叉的规则有两种),在这之前也要根据相反的规则创建两棵树。

Solution:

// Talk is cheap, show me the code
// Created by Misdirection 2021-08-17 10:53:01
// All rights reserved.

#include <iostream>
#include <vector>

using namespace std;

struct Node{
    int key;
    Node *left;
    Node *right;

    Node(int k = -1, Node* l = NULL, Node* r = NULL){
        key = k;
        left = l;
        right = r;
    }

    ~Node(){}
};

vector<int> num, preOrderSeq, postOrderSeq;

void insert(Node *root, int key){
    if(key < root -> key){
        if(root -> left == NULL) root -> left = new Node(key);
        else insert(root -> left, key);
    } 
    else{
        if(root -> right == NULL) root -> right = new Node(key);
        else insert(root -> right, key);
    }
}

void insertM(Node *root, int key){
    if(key >= root -> key){
        if(root -> left == NULL) root -> left = new Node(key);
        else insertM(root -> left, key);
    } 
    else{
        if(root -> right == NULL) root -> right = new Node(key);
        else insertM(root -> right, key);
    }
}


void preOrder(Node *root){
    preOrderSeq.push_back(root -> key);
    if(root -> left != NULL) preOrder(root -> left);
    if(root -> right != NULL) preOrder(root -> right);
}

void postOrder(Node *root){
    if(root -> left != NULL) postOrder(root -> left);
    if(root -> right != NULL) postOrder(root -> right);
    postOrderSeq.push_back(root -> key);
}

int main(){
    int n;
    scanf("%d", &n);
    num.resize(n);

    Node *root, *mroot;
    
    for(int i = 0; i < n; ++i){
        scanf("%d", &num[i]);
        
        if(i == 0){
            root = new Node(num[0]);
            mroot = new Node(num[0]);
        }
        else{
            insert(root, num[i]);
            insertM(mroot, num[i]);
        }
    }

    preOrderSeq.clear();
    preOrder(root);
    bool isBST = true;
    for(int i = 0; i < n; ++i){
        if(preOrderSeq[i] != num[i]){
            isBST = false;
            break;
        }
    }

    if(isBST){
        printf("YES\n");
        postOrder(root);
        for(int i = 0; i < n; ++i){
            if(i == n - 1) printf("%d\n", postOrderSeq[i]);
            else printf("%d ", postOrderSeq[i]);
        }
    }

    else{
        preOrderSeq.clear();
        preOrder(mroot);
        bool isMBST = true;

        for(int i = 0; i < n; ++i){
            if(preOrderSeq[i] != num[i]){
                isMBST = false;
                break;
            }
        }

        if(isMBST == false) printf("NO\n");
        else{
            printf("YES\n");
            postOrder(mroot);
            for(int i = 0; i < n; ++i){
                if(i == n - 1) printf("%d\n", postOrderSeq[i]);
                else printf("%d ", postOrderSeq[i]);
            }
        }
    }
    
    return 0;
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

负反馈循环

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值