1043 Is It a Binary Search Tree (25 分)

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 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



题目大意:给你N个数,让你判断是否是二叉搜索树。这个二叉搜索数有两种情况:1、从小到大的排序 2、从大到小排序

解题:一道基础的数据结构的题目,在网上看了有些大佬的的做法是直接构造树,然后根据先序后续比较确定答案。
这里用另一种方法:先判断是否mirror。在根据不同的情况建树,建树的同时进行非法判断,在父节点一旦使用了右子树的时候,便将父节点标记,伺候父节点不能再用左节点,
当错误子节点找不到位置,便将该错误抛出。如果正常之后直接后续遍历输出。会节省一点空间和时间

/*#include<map>
#include<set>
#include<stack>
#include<math.h>
#include<queue>
#include<string.h>
*/
#include<algorithm>
#include<cstring>
#include<iostream>
#include<stdio.h>
#include<vector>
#define ford(A,B,C) for(int A=B;A<=C;A++)
#define forx(A,B,C) for(int A=B;A<C;A++)
#define inf 0x3f3f3f3f
#define ll  long long int
using namespace std;
struct  tree
{
    int val;
    bool vis = false;
    tree* lson;
    tree* rson;
};
int list[10000];
bool notree = false;
tree* buildtree(tree* A, int val){
    if(A==NULL){
        A=new tree;
        A->val=val;
        A->vis=false;
        A->lson=NULL;
        A->rson=NULL;
        return A;
    }
    if(val<=A->val&&!A->vis){
        A->lson=buildtree(A->lson,val);
    }
    else if(val>=A->val){
        A->vis=true;
        A->rson=buildtree(A->rson,val);
    }
    else notree=true;
    return A;
}



tree* buildtree1(tree* A, int val){
    if(A==NULL){
        A=new tree;
        A->val=val;
        A->vis=false;
        A->lson=NULL;
        A->rson=NULL;
        return A;
    }
    if(val>=A->val&&!A->vis){
        A->lson=buildtree1(A->lson,val);
    }
    else if(val<=A->val){
        A->vis=true;
        A->rson=buildtree1(A->rson,val);
    }
    else{
        notree=true;return A;
    } 
    return A;
}
vector<int> ss;
void behind(tree * A){
    if(A==NULL)
        return;
    behind(A->lson);
    behind(A->rson);
    ss.push_back(A->val);
    return;
}

int main(){
    int N;
    cin>>N;
    forx(i,0,N)
    cin>>list[i];
    bool isgreater=true;
    if(list[0]>=list[N-1])isgreater=false;
    tree* A;A=NULL;
    if(isgreater){
        forx(i,0,N){
        A=buildtree(A,list[i]);
        if(notree)break;
        }
    }
    else {
        forx(i,0,N){
        A=buildtree1(A,list[i]);
        if(notree)break;
        }
    }
    if(!notree){
        cout<<"YES"<<endl;
        behind(A);
        if(!ss.empty()){
            cout<<ss[0];
            forx(i,1,N){
                cout<<' '<<ss[i];
            }
        }
        cout<<endl;
    }
    else{
        cout<<"NO"<<endl;
    }

}

 


posted @ 2019-10-23 10:13  翛宁  阅读(133)  评论(0编辑  收藏  举报