PAT A1043 Is It a Binary Search Tree (25分)

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:

7
8 6 5 7 10 8 11

Sample Output 1:

YES
5 7 6 8 11 10 8

Sample Input 2:

7
8 10 11 8 6 7 5

Sample Output 2:

YES
11 8 10 7 5 6 8

Sample Input 3:

7
8 6 8 5 10 9 11

Sample Output 3:

NO

题意:

输入二叉查找树的结点序列,判断该序列是否为二叉查找树或二叉查找镜像树的先序序列,是则输出该树的后序序列.

思路:

(1)根据给定的序列origin构建二叉查找树;
(2)求出先序遍历二叉查找树序列pre、先序遍历二叉查找镜像树序列preM(先访问原二叉查找树的右子树再访问其左子树)、后序遍历二叉查找树序列post、后序遍历二叉查找镜像树序列postM(先访问原二叉查找树的右子树再访问其左子树),判断并输出.

代码:

#include 
#include 
using namespace std;

struct node{//二叉树的存储结构,链表实现 
	int data;
	node* lchild;
	node* rchild;
};

void insert(node* &root,int x){//在二叉查找树插入结点 
	if(root==NULL){
		root = new node;
		root->data = x;
		root->lchild = root->rchild = NULL;
		return;
	}
	if(root->data>x){
		insert(root->lchild,x);
	}else{//右子树存放数据域大于等于根结点数据域的结点 
		insert(root->rchild,x);
	}
}

node* create(vector<int> data,int n){//创建二叉查找树 
	node* root = NULL;
	for(int i=0;i<n;i++){
		insert(root,data[i]);
	}
	return root;
}

void preorder(node* root,vector<int> &pre){//先序遍历二叉查找树 
	if(root==NULL) return;
	pre.push_back(root->data);
	preorder(root->lchild,pre);
	preorder(root->rchild,pre);
}
void preorderM(node* root,vector<int> &preM){//先序遍历二叉查找镜像树(先访问原二叉查找树的右子树再访问其左子树) 
	if(root==NULL) return;
	preM.push_back(root->data);
	preorderM(root->rchild,preM);
	preorderM(root->lchild,preM);
}
void postorder(node* root,vector<int> &post){//后序遍历二叉查找树 
	if(root==NULL) return;
	postorder(root->lchild,post);
	postorder(root->rchild,post);
	post.push_back(root->data);
}
void postorderM(node* root,vector<int> &postM){//后序遍历二叉查找镜像树(先访问原二叉查找树的右子树再访问其左子树) 
	if(root==NULL) return;
	postorderM(root->rchild,postM);
	postorderM(root->lchild,postM);
	postM.push_back(root->data);
}

int main(){
	vector<int> origin,pre,preM,post,postM;//origin输入序列
	int n,data;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&data);
		origin.push_back(data);	
	}	
	node* root = create(origin,origin.size());//建树 
	preorder(root,pre);//求先序遍历二叉查找树序列
	preorderM(root,preM);//求先序遍历二叉查找镜像树序列 
	postorder(root,post);//求后序遍历二叉查找树序列 
	postorderM(root,postM);//求后序遍历二叉查找镜像树序列 
	if(origin==pre){
		printf("YES\n");
		for(int i=0;i<post.size();i++){
			printf("%d",post[i]);
			if(i!=post.size()-1) printf(" ");
		}
	}else if(origin==preM){
		printf("YES\n");
		for(int i=0;i<postM.size();i++){
			printf("%d",postM[i]);
			if(i!=postM.size()-1) printf(" ");
		}
	}else{
		printf("NO\n");
	}
	return 0;
}

你可能感兴趣的:(PAT甲级,PAT,程序设计,c++,算法,数据结构)