A1043 Is It a Binary Search Tree (25 point(s))

二叉树的建立,先序,后序遍历

1. 原文

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

2. 解析

3. AC代码

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include<cstdio>
#include<vector>
using namespace std;
vector<int> mirrorpre,mirrorpost,pre,post,origin;
struct node
{
int data;
node *lchild,*rchild;
};
node* newNode(int x){
node* root=new node;
root->data=x;
root->lchild=root->rchild=NULL;
return root;
}
void create(int x,node* &root){
if (root==NULL)
{
root=newNode(x);
return;
}else if(root->data>x){
create(x,root->lchild);
}else{
create(x,root->rchild);
}
}
void preorder(node* root){
if (root==NULL)
{
return;
}
pre.push_back(root->data);
preorder(root->lchild);
preorder(root->rchild);
}
void postorder(node* root){
if (root==NULL)
{
return;
}
postorder(root->lchild);
postorder(root->rchild);
post.push_back(root->data);
}
void mipre(node* root){
if (root==NULL)
{
return;
}
mirrorpre.push_back(root->data);
mipre(root->rchild);
mipre(root->lchild);
}
void mipost(node* root){
if (root==NULL)
{
return;
}
mipost(root->rchild);
mipost(root->lchild);
mirrorpost.push_back(root->data);
}
int main()
{
int n;
scanf("%d",&n);
int a;
node* root=NULL;
for (int i = 0; i < n; ++i)
{
scanf("%d",&a);
origin.push_back(a);
create(a,root);
}
preorder(root);
postorder(root);
mipre(root);
mipost(root);
if (origin==pre||origin==mirrorpre)
{
printf("YES\n");
if (origin==pre)
{
for (int i = 0; i < post.size(); ++i)
{
printf("%d",post[i]);
if (i<post.size()-1)
{
printf(" ");
}else{
printf("\n");
}
}
}else{
for (int i = 0; i < mirrorpost.size(); ++i)
{
printf("%d",mirrorpost[i]);
if (i<mirrorpost.size()-1)
{
printf(" ");
}else{
printf("\n");
}
}

}
}else{
printf("NO\n");
}
return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1043/
  • 发布时间: 2019-09-03 12:34
  • 更新时间: 2021-10-29 14:00
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!