A1043 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 (≤). 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


  1 #include<cstdio>
  2 struct node{
  3     int data;
  4     node *lchild;
  5     node *rchild;
  6 };
  7 node *newNode(int x){//生成新的节点
  8     node *rt=new node;
  9     rt->data=x;
 10     rt->lchild=rt->rchild=NULL;
 11     return rt;
 12 }
 13 void insert(node* &rt,int x){
 14     if(rt==NULL){//在BST树中插入节点
 15         rt=newNode(x);
 16         return;
 17     }
 18     if(rt->data>x){
 19         insert(rt->lchild,x);
 20     }else{
 21         insert(rt->rchild,x);
 22     }
 23 }
 24 node *create(int data[],int n){//创建BST树
 25     node *rt=NULL;
 26     for(int i=0;i<n;i++){
 27         insert(rt,data[i]);
 28     }
 29     return rt;
 30 }
 31 node *swapBst(node *rt){//利用前序递归遍历,实现将所有节点的左子树
 32     if(rt==NULL){//和右子树相互交换
 33         return NULL;
 34     }else{
 35         node *temp=rt->lchild;
 36         rt->lchild=rt->rchild;
 37         rt->rchild=temp;
 38         swapBst(rt->lchild);
 39         swapBst(rt->rchild);
 40     }
 41     return rt;
 42 }
 43 int* preOrder(node *rt,int pre[],int &k){
 44     if(rt==NULL){//将BST树遍历输出到指定数组中
 45         return NULL;
 46     }else{
 47         pre[k++]=rt->data;
 48         preOrder(rt->lchild,pre,k);
 49         preOrder(rt->rchild,pre,k);
 50     }
 51     return pre;
 52 }
 53 bool isP(int data[],int temp[],int n){
 54     for(int i=0;i<n;i++){//判定两个数组中元素是否对应相等
 55         if(data[i]!=temp[i]){
 56             return false;
 57         }
 58     }
 59     return true;
 60 }
 61 void postOrder(node *rt,int &n){//后序遍历输出二叉树
 62     if(rt==NULL){
 63         return;
 64     }else{
 65         postOrder(rt->lchild,n);
 66         postOrder(rt->rchild,n);
 67         if(n!=1){
 68             printf("%d ",rt->data);
 69             n--;
 70         }else{
 71             printf("%d",rt->data);
 72         }
 73     }
 74 }
 75 int main()
 76 {
 77     int n;
 78     int data[1100];
 79     int pre[1100];
 80     int mir[1100];
 81     scanf("%d",&n);
 82     for(int i=0;i<n;i++){
 83         scanf("%d",&data[i]);
 84     }
 85     node *rt=create(data,n);
 86     int k=0;
 87     preOrder(rt,pre,k);
 88     if(isP(data,pre,n)){
 89         printf("YES\n");
 90         postOrder(rt,n);
 91         return 0;
 92     }
 93     node *mirr=swapBst(rt);
 94     k=0;
 95     preOrder(mirr,mir,k);
 96     if(isP(data,mir,n)){
 97         printf("YES\n");
 98         postOrder(rt,n);
 99         return 0;
100     }
101     printf("NO\n");
102     return 0;
103 }
View Code

 

Mist Note:本题考察二叉搜索树(BST)的相关算法,首先要学会利用给定序列建立BST,建立之后会递归遍历,

灵活利用递归遍历交换所有节点的左右子树。

posted on 2019-02-14 15:30  Aldrich_2020  阅读(238)  评论(0编辑  收藏  举报