题目:
对于二叉搜索树,我们规定任一结点的左子树仅包含严格小于该结点的键值,而其右子树包含大于或等于该结点的键值。如果我们交换每个节点的左子树和右子树,得到的树叫做镜像二叉搜索树。
现在我们给出一个整数键值序列,请编写程序判断该序列是否为某棵二叉搜索树或某镜像二叉搜索树的前序遍历序列,如果是,则输出对应二叉树的后序遍历序列。
输入格式:
输入的第一行包含一个正整数N(≤1000),第二行包含N个整数,为给出的整数键值序列,数字间以空格分隔。
输出格式:
输出的第一行首先给出判断结果,如果输入的序列是某棵二叉搜索树或某镜像二叉搜索树的前序遍历序列,则输出YES,否侧输出NO。如果判断结果是YES,下一行输出对应二叉树的后序遍历序列。数字间以空格分隔,但行尾不能有多余的空格。
输入样例1:
7
8 6 5 7 10 8 11
输出样例1:
YES
5 7 6 8 11 10 8
输入样例2:
7
8 6 8 5 10 9 11
输出样例2:
NO
一道十分有难度的题目。
看了书上的实验分析,我觉得可以用动态规划实现,然后写代码加上调试花了接近四个小时,到最后实在是调试不成功了,加上代码也写的尾大不掉,遂放弃这一条思路,跑步回来后一切推倒重来,老老实实用建树实现需求。
我真的觉得这道题难度有些割裂——既需要实现判断是否二叉搜索树,又需要建树,然后根据前序输出后序,最关键的是镜像二叉搜索树到底和普通二叉搜索树有什么不同、哪些代码可以重载,任何一个都可能卡住,属实难题。
第一次代码如下,以后有机会改正确:
using namespace std;
#define MAXSIZE 1000
int ans[MAXSIZE];
int num = 0,jud=1;
//这种方法相当于动态规划,随时读入,随时输出
void creatlastorder(int bintree[],int n,int tag) {//tag 1表示二叉搜索树,2表示镜像搜索树,3表示不确定
int tl[MAXSIZE + 1] = {NULL}, tr[MAXSIZE + 1] = {NULL}, left = 0, right = 0,test,jud2=0,firstbigger=1, firstsmaller = 1;
if(tag==3)
if (bintree[0] < bintree[1])
tag = 2;
else
tag = 1;
for (int i = 1; i < n; i++) {
if (bintree[i] < bintree[0]&&jud2<=2) {
firstsmaller = 1;
if (firstbigger) {
jud2++;
firstbigger = 0;
}
tl[left++] = bintree[i];
}
else if (bintree[i] >= bintree[0] && jud2 <= 2) {
firstbigger = 1;
if (firstsmaller) {
jud2++;
firstsmaller = 0;
}
tr[right++] = bintree[i];
}
else {
cout << "NO" << endl;
jud = 0;
return;
}
}
if (tl[0] == NULL && tr[0] == NULL) {
if(bintree[0]!=NULL)
ans[num++] = bintree[0];
return;
}
else if (bintree[0] != NULL && bintree[1] != NULL && tl[0] != NULL && tr[0] != NULL) {
if (bintree[0] <=tr[0]&& bintree[0]>tl[0])
test = 1;
else
test = 2;
}
else if (bintree[0] != NULL && bintree[1] != NULL && tl[0] == NULL && tr[0] != NULL) {
if (bintree[0] <= tr[0])
test = 1;
else
test = 2;
}
else if (bintree[0] != NULL && bintree[1] != NULL && tl[0] != NULL && tr[0] == NULL) {
if (bintree[0] > tl[0])
test = 1;
else
test = 2;
}
if (tag != test) {
cout << "NO" << endl;
jud = 0;
return;
}
if (tag == 1) {
creatlastorder(tl, left, tag);
creatlastorder(tr, right, tag);
}
else {
creatlastorder(tr, right, tag);
creatlastorder(tl, left, tag);
}
ans[num++] = bintree[0];
return;
}
int main() {
int n, preorder[MAXSIZE + 1] = {0},iffirst=1;
cin >> n;
if (n == 1) {
cin >> n;
cout << "YES" << endl;
cout << n;
return 0;
}
else
for (int i = 0; i < n; i++)
cin >> preorder[i];
creatlastorder(preorder,n, 3);
if (jud) {
cout << "YES" << endl;
cout << ans[0];
for (int i = 1; i < num; i++)
cout << " " << ans[i];
}
return 0;
}
AC代码:
#include <cstdio>
#include <malloc.h>
#include <cstring>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <iostream>
using namespace std;
#define LEN sizeof(Node)
const int maxn = 1017;
typedef struct Node
{
int num;
struct Node *right, *left;
} Node;
queue<int >Q1;
queue<int >Q2;
void swap(Node *&T)//交换每个节点的左右子树
{
Node *tt = NULL;
if(!T)
return ;
swap(T->left);
swap(T->right);
tt = T->left;
T->left = T->right;
T->right = tt;
}
void insert(Node * &T, int data)//建立二叉树
{
if(!T)
{
T = (Node*)malloc(LEN);
T->num = data;
T->left = T->right = NULL;
return ;
}
else
{
if(data >= T->num)
{
insert(T->right, data);
}
else
{
insert(T->left, data);
}
}
}
void Pre_order(Node * &T)//先序遍历
{
if(!T)
return ;
Q1.push(T->num);
Pre_order(T->left);
Pre_order(T->right);
}
void Later_order(Node * &T)//后续遍历
{
if(!T)
return ;
Later_order(T->left);
Later_order(T->right);
Q2.push(T->num);
}
int main()
{
int n;
int a[maxn];
int flag = 0;
Node *root = NULL;
scanf("%d",&n);
for(int i = 0; i < n; i++)
{
scanf("%d",&a[i]);
insert(root, a[i]);
}
Pre_order(root);//先序遍历
for(int i = 0; i < n; i++)
{
int tt = Q1.front();
if(a[i] != tt)//不是二叉搜索数的先序遍历
{
flag++;
while(!Q1.empty())
{
Q1.pop();
}
break;
}
Q1.pop();
}
if(flag)
{
swap(root);//交换根值,变为镜像二叉搜索树
Pre_order(root);//再次先序遍历
for(int i = 0; i < n; i++)
{
int tt = Q1.front();
if(a[i] != tt)//也不是镜像二叉树的先序遍历
{
flag++;
while(!Q1.empty())
{
Q1.pop();
}
break;
}
Q1.pop();
}
}
if(flag == 2)//不是先序遍历
{
printf("NO\n");
}
else
{
printf("YES\n");
Later_order(root);//后续遍历
for(int i = 0; i < n-1; i++)
{
int tt = Q2.front();
printf("%d ",tt);
Q2.pop();
}
printf("%d\n",Q2.front());
}
return 0;
}