PAT1043.Is It a Binary Search Tree

题目

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:

1
2
7
8 6 5 7 10 8 11

Sample Output 1:

1
2
YES
5 7 6 8 11 10 8

Sample Input 2:

1
2
7
8 10 11 8 6 7 5

Sample Output 2:

1
2
YES
11 8 10 7 5 6 8

Sample Input 3:

1
2
7
8 6 8 5 10 9 11

Sample Output 3:

1
NO

代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <cstdio>
#include <vector>
using namespace std;
struct node{
    int data;
    node *left,*right;
};
vector<int> input,pre,mpre,post;
int isMirror=0,notTree=0;


void insert(node* &root,int value)
{
    if(root==NULL)
    {
        root=new node;
        root->data=value;
        root->left=NULL;root->right=NULL;
        return;
    }
    if(value < root->data)
        insert(root->left,value);
    else
        insert(root->right,value); // >=
}

void preorder(node *root)
{
    if (root==NULL)
        return;
    pre.push_back(root->data);
    preorder(root->left);
    preorder(root->right);
}
void mpreorder(node *root)
{
    if (root==NULL)
        return;
    mpre.push_back(root->data);
    mpreorder(root->right);
    mpreorder(root->left);
}
void postorder(node *root)
{
    if (root==NULL)
        return;
    postorder(root->left);
    postorder(root->right);
    post.push_back(root->data);
}
void mpostorder(node *root)
{
    if (root==NULL)
        return;
    mpostorder(root->right);
    mpostorder(root->left);
    post.push_back(root->data);
}
int main()
{
    int n;
    node *root=NULL;
    scanf("%d",&n);
    input.resize(n);
    for(int i=0;i<n;i++)
        scanf("%d",&input[i]);
    //build bst
    for(int i=0;i<n;i++)
        insert(root,input[i]);
    preorder(root);
    mpreorder(root);
    if(pre==input)
        isMirror=0;
    else if(mpre==input)
        isMirror=1;
    else
        notTree=1;

    if(notTree)
        printf("NO");
    else
    {
        if(isMirror)
            mpostorder(root);
        else
            postorder(root);
        printf("YES\n%d",post[0]);
        for(int i=1;i<n;i++)
            printf(" %d",post[i]);
    }

    return 0;
}

分析

重建搜索二叉树并进行先序遍历,进行先序搜索和镜像先序搜索判断是否是搜索树以及是否镜像。然后进行后序遍历输出结果。

  1. 搜索二叉树的中序遍历有序
  2. 中序遍历结果的第一个节点就是二叉树的根节点
Licensed under CC BY-NC-SA 4.0
Built with Hugo
主题 StackJimmy 设计