PAT甲级刷题记录——1043 Is It a Binary Search Tree (25分)

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 YESif the sequence is the preorder traversal sequence of a BST or the mirror image of a BST, or NOif 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

思路

这题还是比较常规的,就是通过题目给出的数据来建立一棵BST。

然后还要再判断一下题目给的序列(题目给的那行数据又是初始序列又是数据哈)是不是这棵BST树的先序或者是不是这棵BST树镜像版的先序(镜像版的先序很简单,就是让建好的那棵BST遍历时先往右子树走就行了,一般的先序是根左右,那么镜像就是根右左)。如果确实是BST(或者镜像BST)的先序,那么对应输出它的后序,如果不是,输出NO。

这里主要说一下,怎么输出镜像BST的后序。一般情况下,如果题目给出的序列就是一棵普通BST的先序,那么直接按照套路遍历后序就行了,如果题目给出的序列满足的是镜像BST的先序,那么就说明我们后序遍历的时候也要反过来(这里要弄清楚的一点是,我们通过数据来建立的只是一棵普通的BST,也就是左<根<右,所谓镜像版的东西,其实我们并没有建立相应的镜像树,只不过是遍历的时候倒过来而已,这和建立一棵镜像树并按照原来顺序遍历结果是一样的,但是要方便很多),也就是右左根

这里要注意的一点是,插入函数在root->data==x时,不要写return,否则相同元素插不上去(比如样例1,如果写了相等时return这句话,就会只有一个8)。

代码

#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<string.h>
#include<iostream>
using namespace std;
const int maxn = 1010;
struct node{
    int data;
    node* lchild;
    node* rchild;
};
vector<int> initial;
vector<int> preorderBST;
vector<int> preorderMBST;//镜像BST的先序
int Data[maxn] = {0};
node* newNode(int x){
    node* Node = new node;
    Node->data = x;
    Node->lchild = Node->rchild = NULL;
    return Node;
}
void Insert(node* &root, int x){
    if(root==NULL){
        root = newNode(x);
        return;
    }
    if(x<root->data) Insert(root->lchild, x);
    else Insert(root->rchild, x);
}
node* create(int data[], int len){
    node* root = NULL;
    for(int i=0;i<len;i++) Insert(root, data[i]);
    return root;
}
void preorderB(node* root){
    if(root==NULL) return;
    preorderBST.push_back(root->data);
    preorderB(root->lchild);
    preorderB(root->rchild);
}
void preorderMB(node* root){
    if(root==NULL) return;
    preorderMBST.push_back(root->data);
    preorderMB(root->rchild);
    preorderMB(root->lchild);
}
void postorderB(node* root, int &cnt){
    if(root==NULL) return;
    postorderB(root->lchild, cnt);
    postorderB(root->rchild, cnt);
    if(cnt==0){
        printf("%d", root->data);
        cnt++;
    }
    else{
        printf(" %d", root->data);
        cnt++;
    }
}
void postorderMB(node* root, int &cnt){
    if(root==NULL) return;
    postorderMB(root->rchild, cnt);
    postorderMB(root->lchild, cnt);
    if(cnt==0){
        printf("%d", root->data);
        cnt++;
    }
    else{
        printf(" %d", root->data);
        cnt++;
    }
}
int main(){
    int N;
    scanf("%d", &N);
    for(int i=0;i<N;i++){
        int tmp;
        scanf("%d", &tmp);
        Data[i] = tmp;
        initial.push_back(tmp);
    }
    node* root = create(Data, N);
    preorderB(root);
    preorderMB(root);
    if(initial==preorderBST){
        printf("YES\n");
        int cnt = 0;
        postorderB(root, cnt);
    }
    else if(initial==preorderMBST){
        printf("YES\n");
        int cnt = 0;
        postorderMB(root, cnt);
    }
    else printf("NO");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值