pat(甲)1043(二叉搜索树)

1043 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、树的左子树的节点小于根节点的值;

2、树的右子树的节点大于根节点的值。

将二叉搜索树的左右子树交换得到它的镜像树。

如果二叉搜索树的前序遍历或者二叉搜索树镜像的前序遍历与之前输入的序列相同,就输出“YES”就输出树的后序遍历,否则输出“NO”。

思路:先存储序列,然后构建搜索二叉树及其镜像,再得到他们的前序遍历,如果符和,输出其后序遍历。

#include<iostream>
#include<vector>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct Node{
	int data;
	struct Node *left,*right;
}; 
typedef struct Node* N;

N Creat()
{
	N tp=new Node;
	tp->left=tp->right=NULL;
	return tp;
}

N insert1(N t,int tp)
{
	if(t==NULL)
	{
		t=Creat();
		t->data=tp;
	}
	else 
	{
		if(tp<t->data) t->left=insert1(t->left,tp);
		else t->right=insert1(t->right,tp);
	}
	return t;
} 
 
N insert2(N t,int tp)
{
	if(t==NULL)
	{
		t=Creat();
		t->data=tp;
	}
	else
	{
		if(tp<t->data) t->right=insert2(t->right,tp);
		else t->left=insert2(t->left,tp);
	}
	return t;
}

void pertravel(N t,vector<int> &vc)
{
	vc.push_back(t->data);
	if(t->left!=NULL) pertravel(t->left,vc);
	if(t->right!=NULL) pertravel(t->right,vc);
}

void postravel(N t,vector<int> &vc)
{
	if(t->left!=NULL) postravel(t->left,vc);
	if(t->right!=NULL) postravel(t->right,vc);
	vc.push_back(t->data);
}

int main(void)
{
	vector <int> v1,v2,v3,v4;
	int i,n,tp;
	N t1=NULL,t2=NULL;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf("%d",&tp);
		v1.push_back(tp);
		t1=insert1(t1,tp);
		t2=insert2(t2,tp);
	}
	
	pertravel(t1,v2);
	pertravel(t2,v3);
	if(v1!=v2&&v1!=v3)
	{
		printf("NO\n");
	}
	else
	{
		printf("YES\n");
		if(v1==v2) 
		{
			postravel(t1,v4);
			for(i=0;i<n-1;i++) printf("%d ",v4[i]);
			printf("%d\n",v4[i]);
		}
		else if(v1==v3)
		{
			postravel(t2,v4);
			for(i=0;i<n-1;i++) printf("%d ",v4[i]);
			printf("%d\n",v4[i]);
		}
	}
	return 0;
}

参考文章:https://blog.csdn.net/xtzmm1215/article/details/38647991

猜你喜欢

转载自blog.csdn.net/qq_41829060/article/details/82661753