[PAT A1043]Is is a Binary Search Tree

1、问题描述

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.

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

2、思路

2.1 二叉排序树

判断一个二叉树是否为二叉排序树,将给定的先序序列还原成二叉排序树,再得到生成的二叉排序树的先序序列与给定的序列对比,如果相等则给定的序列为二叉排序树。假如给定的序列不是二叉排序树序列,那么生成出来的二叉排序树的先序序列一定和给定的序列不同。

2.2 二叉排序树镜像

判断一个二叉树是否为二叉排序树镜像,思路和上面完全一致,只是在生成的二叉树的先序序列和后续序列时稍微有点不一样。二叉排序树镜像的先序序列为DRL(正常的二叉树为DLR),后序序列为RLD(正常的二叉树为LRD)

3、代码实现

#include<iostream>
#include<vector>

using namespace std;
#define MaxSize 50
typedef int ElemType;
typedef struct node{
    
    
    ElemType data;
    node * lchild;
    node * rchild;
}BiNode, *BinTree;
vector<ElemType> Pre,Post,MPre,MPost;


void Insert(BinTree &T, ElemType x);
void LDR(BinTree T);
void PreOrder(BinTree T, vector<ElemType> &Pre);
void PostOrder(BinTree T, vector<ElemType> &Post);
void MPreOrder(BinTree T, vector<ElemType> &MPre);
void MPostOrder(BinTree T, vector<ElemType> &MPost);


int main(){
    
    
    int n;
    cin >> n;
    BinTree T = NULL;
    ElemType t;
    vector<ElemType> original;

    for(int i = 0; i < n; i++){
    
    
        cin >> t;
        original.push_back(t);
        Insert(T,t);
    }
    PreOrder(T,Pre);
    PostOrder(T,Post);
    MPreOrder(T,MPre);
    MPostOrder(T,MPost);


    if(original==Pre){
    
    
        cout << "YES" << endl;
        for(auto it = Post.begin(); it != Post.end(); it++){
    
    
            cout << *(it) << " ";
        }
    }else if(original == MPre){
    
    
        cout << "YES" << endl;
        for(auto it = MPost.begin(); it != MPost.end(); it++){
    
    
            cout << *(it) << " ";
        }
    }else{
    
    
        cout << "NO" << endl;
    }

    return 0;
}
void Insert(BinTree &T, ElemType  x){
    
    
    if (T == NULL){
    
    
        T = new BiNode ;
        T->data = x;
        T->lchild = T->rchild = NULL;
    }else{
    
    
        if(x < T->data){
    
    
            Insert(T->lchild, x);
        }else{
    
    
            Insert(T->rchild, x);
        }
    }

}

void LDR(BinTree T){
    
    
    if(T){
    
    
        LDR(T->lchild);
        cout << T->data << " ";
        LDR(T->rchild);
    }
}

void PreOrder(BinTree T, vector<ElemType> &Pre){
    
    
    if(T){
    
    
        Pre.push_back(T->data);
        PreOrder(T->lchild,Pre);
        PreOrder(T->rchild,Pre);
    }
}

void PostOrder(BinTree T, vector<ElemType> &Post){
    
    
    if(T){
    
    
        PostOrder(T->lchild,Post);
        PostOrder(T->rchild,Post);
        Post.push_back(T->data);
    }
}

void MPreOrder(BinTree T, vector<ElemType> &MPre){
    
    
    if(T){
    
    
        MPre.push_back(T->data);
        MPreOrder(T->rchild, MPre);
        MPreOrder(T->lchild, MPre);
    }
}
void MPostOrder(BinTree T, vector<ElemType> &MPost){
    
    
    if(T){
    
    
        MPostOrder(T->rchild,MPost);
        MPostOrder(T->lchild, MPost);
        MPost.push_back(T->data);
    }
}

猜你喜欢

转载自blog.csdn.net/xdg15294969271/article/details/120495955