PAT 甲级 A1043 Is it a Binary Search Tree

PAT,你伤我好深


 

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

首先呢,这题是算法笔记上的例题,然后,我,没有看懂,他写的什么,然后,就,看了,一个小时。

主要是看不懂晴神笔记里为什么能直接把题目给的前序序列当作插入的序列,一棵BST的插入序列和前序遍历的序列为什么是相同的……思考了一个多小时终于从崩溃中走出来,直接按自己笔算的思路写了^^

おれは人間をやめるぞ! ジョジョ──ッ!!

我的垃圾思路如下:

1.反证法,假设是BST,因为case给的是preOrder,那么pre[1]>=pre[0]的话就说明是镜像BST,反之是常规BST。

2.BST是可以只有前序/后序就能建唯一的树的。根据BST的性质和pre[1]的大小,可以确定序列第一位(pre[0])是根结点,左右子树的根节点是pre[1]和pre序列里第一个大于(镜像是小于)pre[0]的结点。

3.根据第二条的递归式,对序列进行递归建树。

4.建立的树如果是BST,那么他的中序遍历一定有序,根据这点进行是否是BST的判断。

5.如果是BST,再进行后序遍历,输出结果;不是就直接输出NO。

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct node {
	int v;
	node* left=NULL;
	node* right=NULL;
};
node* create(int L, int R, int pre[],bool mirror) {
	if (L > R) return NULL;
	node* root = new node;
	root->v = pre[L];
	int i = L + 1;
	for (; i <= R; i++){            //找左右树在哪儿
		if (pre[i] >= root->v &&mirror==false) break;        //如果是普通BST的处理
		else if (pre[i]< root->v && mirror == true) break;   //如果是镜像BST的处理
	}
	int leftNum = i - (L + 1);
	root->left = create(L + 1, L + leftNum, pre,mirror);
	root->right = create(L + leftNum + 1, R, pre,mirror);
	return root;
}
void inOrder(node* root, vector<int> &in) {
	if (root == NULL) return;
	inOrder(root->left,in);
	in.push_back(root->v);
	inOrder(root->right,in);
}
void postOrder(node*root, vector<int> &post) {
	if (root == NULL) return;
	postOrder(root->left, post);
	postOrder(root->right, post);
	post.push_back(root->v);
}
int main() {
	int N;
	int pre[1005];
	scanf("%d", &N);
	for (int i = 0; i < N; i++)
		scanf("%d", &pre[i]);
	bool mirror = true;
	if (pre[1] < pre[0]) mirror = false;
	node* root = create(0, N - 1, pre,mirror);
	vector<int> in;
	inOrder(root, in);
	for (int i = 1; i < in.size(); i++) {
		if (in[i] > in[i - 1]) mirror = false;
	}
	if (mirror == false) {
		mirror = true;
		for (int i = 1; i < in.size(); i++)
			if (in[i] < in[i - 1]) mirror = false;
	}
	vector<int> post;
	postOrder(root, post);
	if (mirror == true) cout << "YES\n";
	else {
		cout << "NO"; 
		return 0;
	}
	for (int i = 0; i < post.size(); i++){
		cout << post[i];
		if (i != post.size() - 1) cout << " ";
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值