PAT 1032 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.

输入描述:
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.


输出描述:
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.

输入例子:
7
8 6 5 7 10 8 11

输出例子:
YES
5 7 6 8 11 10 8

思路

  • 1.先序遍历是先根再左再右,即输入例子中第一个是根,然后后面接几个左边的,再接几个右边的,本来无法判断哪些是左边哪些是右边,但是由于它又是二叉搜索树,即左边要小于根,右边要大于根,所以当找到一个大于根了,那么要求后面的也要大于根,这是由先序遍历+搜索树的性质决定的,然后对于左右子树,也同样要满足这样的性质。而对于Mirror,反过来就行了。
  • 2.对于为镜像和不为镜像的都可以写到一个函数里面。

我出错的一些点

  • 1.递归不熟悉,这种问题应该第一眼就要想到用递归去判断和构建树的.
  • 2.树的构建也不熟悉。

代码

#include<iostream>
#include<vector>
#include<string>
#include<iomanip>
#include<sstream>
#include<algorithm>
using namespace std;
vector<int> a;
int n;
struct tree
{
    int value;
    tree *left, *right;
    tree(int k) :left(NULL), right(NULL), value(k){}
};
//先序加BST的性质决定了找出一个数比他大了(或者小),那么后面的就要一直比他大或者小,不然就违反了BST和先序遍历的性质了
bool isBST(int l,int r,bool isMirror)
{
    if (l >= r)return true;
    int mid = l + 1;
    //找到第几个开始比他大(或者小)
    while (mid<=r&&(a[mid] < a[l]!=isMirror))mid++;
    //然后再判断后面的是否符合继续比他大(或者小)
    for (int i = mid; i <= r; i++)
    {
        if (a[i] < a[l]!=isMirror)
            return false;
    }
    return isBST(l + 1, mid - 1,isMirror) && isBST(mid, r,isMirror);
}
tree* creat(int l,int r,bool isMirror)
{

    if (l > r) return NULL;
    tree *root = new tree(a[l]);
    int mid = l + 1;
    while (mid<=r&&(a[mid] < a[l]!=isMirror))mid++;
    root->left = creat(l + 1, mid - 1,isMirror);
    root->right = creat(mid, r,isMirror);
    return root;
}
void post(tree *root, vector<int>& c)
{
    if (root == NULL)
        return;
    post(root->left, c);
    post(root->right,c);
    //cout << root->value << endl;
    c.push_back(root->value);
}
int main()
{
    cin >> n;
    a.resize(n);
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    bool isL = isBST(0, n - 1, false);
    bool isR = isBST(0, n - 1, true);
    if (isL || isR)
    {
        cout << "YES" << endl;
        vector<int> c;
        tree *b=NULL;
        if (isL)
            b = creat(0, n - 1, false);
        else if (isR)
            b = creat(0, n - 1, true);
        post(b, c);
        for (int i = 0; i < int(c.size()); i++)
        {
            if (i == 0)
                cout << c[i];
            else
                cout << " " << c[i];
        }
        cout << endl;
    }
    else
        cout << "No Solution" << endl;


    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值