题目
考察点
二叉搜索树的性质
思路
从样例1中可以知道,二叉搜索树的插入序列就是其先序遍历所得到的序列,因此可以通过题目给出的先序遍历,构造二叉搜索树
样例1的树结构(正常的BST)
8
6 10
5 7 8 11
例2的树结构(BST的镜像)
8
10 6
11 8 7 5
例子3的树结构为错误的二叉树结构不做展开。
通过1和2的树结构,可知对镜像树的先序遍历时交换左右子树的的遍历顺序即可。具体可参考代码
代码
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Blog: https://blog.csdn.net/weixin_41234001
*
* Author: DoBetter
*
* Time: 2019.12.01
*
* Describe: 判断这串序列是否是该二叉排序树的先序序列或是该二叉排序树的镜像树的先序序列
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
#include <iostream>
#include <vector>
using namespace std;
struct node
{
int data;
node* lchild;
node* rchild;
};
node* NewNode(int i)
{
node* root = new node;
root->data = i;
root->lchild = root->rchild = NULL;//左右孩子置为空
return root;
}
//插入
void insert(node*& root, int x)
{
if (root==NULL)
{
root = NewNode(x);
return;
}
if (x<root->data)
{
insert(root->lchild, x);
}
else
{
insert(root->rchild, x);
}
}
vector<int> origin, pre, preM, post, postM;
//BST的先序遍历
void preOrder(node* root)
{
if (root==NULL)
{
return;
}
pre.push_back(root->data);//加入到序列
preOrder(root->lchild);
preOrder(root->rchild);
}
//BST的Mirror的先序遍历
void preMOrder(node* root)
{
if (root == NULL)
{
return;
}
preM.push_back(root->data);//加入到序列
preMOrder(root->rchild);
preMOrder(root->lchild);
}
//BST的后序遍历
void postOrder(node* root)
{
if (root==NULL)
{
return;
}
postOrder(root->lchild);
postOrder(root->rchild);
post.push_back(root->data);
}
//BST的Mirror的后序遍历
void postMOrder(node* root)
{
if (root == NULL)
{
return;
}
postMOrder(root->rchild);
postMOrder(root->lchild);
postM.push_back(root->data);
}
int main()
{
int n;
node* root = NULL;
scanf("%d", &n);//输入n
for (int i=0;i<n;i++)
{
int data;
scanf("%d", &data);
origin.push_back(data);
insert(root, data);//插入
}
//调用四次遍历
preOrder(root);
preMOrder(root);
postOrder(root);
postMOrder(root);
if (origin==pre)
{
printf("YES\n");
for (int i=0;i<n;i++)
{
printf("%d", post[i]);
if (i<n-1)
{
printf(" ");
}
}
}
else if (origin==preM)
{
printf("YES\n");
for (int i=0;i<n;i++)
{
printf("%d", postM[i]);
if (i<n-1)
{
printf(" ");
}
}
}
else
{
printf("NO");
}
return 0;
}