PAT 1043

题目

PAT 1043

考察点

二叉搜索树的性质

思路

从样例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;
}

结果

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值