PAT (Advanced Level) Practice 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

给出先序遍历,问此树是否是二叉搜索树,是输出 Y E S YES YES和后序遍历,否输出 N O NO NO
思路:
不管它先序这个条件,直接按二叉搜索树建树,然后进行先序遍历和镜像的先序遍历(即先遍历右子树再遍历左子树),之后也看看跟输入的数据一样不,如果正常的一样就输出正常的后序,镜像的一样就输出镜像的后序(即先遍历右子树再遍历左子树)。

#include <bits/stdc++.h>
#include <limits.h>
#include <math.h>
#include <memory.h>
#include <stdio.h>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ms(x, n) memset(x, n, sizeof(x))
#define wht() \
    int _;    \
    for (scanf("%d", &_); _; _--)
#define For(i, a, b) for (int i = a; i <= (b); ++i)
#define Rof(i, a, b) for (int i = a; i >= (b); --i)
#define ioss ios::sync_with_stdio(false)
#define all(a) a.begin(), a.end()
#define ll long long
// #define input(x) scanf("%lld", &x);
// #define print(x) printf("%lld", x);
// #define println(x) printf("%lld\n", x);
typedef vector<int> vi;
typedef pair<int, int> pii;

const int maxn = 2e6 + 7;
const int maxm = 5e3 + 7;
const int MOD = 1e9 + 7;
const int mod = 998244353;

int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline int input() {
    int x = 0, f = 0;
    char ch = 0;
    while (!isdigit(ch)) f |= ch == '-', ch = getchar();
    while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
    return f ? -x : x;
}

inline void print(int x) {
    if (x < 0) putchar('-'), x = -x;
    int sz[20], tot = 0;
    while (x) sz[++tot] = x % 10, x /= 10;
    while (tot) putchar(sz[tot--] ^ 48);
}
const int N = 1e5 + 5;
int n, m, k;
int a[N], sum[N];
int f[N];

struct node {
    int val;
    node *left;
    node *right;
};

void build(node *&r, int val) {
    if (r == nullptr) {
        r = new node;
        r->val = val;
        r->left = r->right = nullptr;
        return;
    }
    if (val < r->val)
        build(r->left, val);
    else
        build(r->right, val);
}

vi pre;
void predfs(node *r) {
    if (r == nullptr) return;
    pre.push_back(r->val);
    predfs(r->left);
    predfs(r->right);
}

vi preT;
void preTdfs(node *r) {
    if (r == nullptr) return;
    preT.push_back(r->val);
    preTdfs(r->right);
    preTdfs(r->left);
}

vi postT;
void postTdfs(node *r) {
    if (r == nullptr) return;
    postTdfs(r->right);
    postTdfs(r->left);
    postT.push_back(r->val);
}

vi post;
void postdfs(node *r) {
    if (r == nullptr) return;
    postdfs(r->left);
    postdfs(r->right);
    post.push_back(r->val);
}

void work() {
    n = input();
    node *root = nullptr;
    vi ans;
    For(i, 1, n) {
        int x = input();
        ans.push_back(x);
        build(root, x);
    }
    int flag = 0;
    predfs(root);
    preTdfs(root);
    if (pre == ans) flag = 1;
    if (preT == ans) flag = 2;

    if (!flag) {
        puts("NO");
        return;
    }
    puts("YES");
    postdfs(root);
    postTdfs(root);
    if (flag == 1) {
        int fir = 0;
        for (auto i : post) {
            if (fir) putchar(' ');
            fir = 1;
            print(i);
        }
    } else {
        int fir = 0;
        for (auto i : postT) {
            if (fir) putchar(' ');
            fir = 1;
            print(i);
        }
    }
}

signed main() {
#ifndef ONLINE_JUDGE
    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);
#endif
    work();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值