PTA甲级 1043 Is It a Binary Search Tree (25分) 树的遍历

强烈推荐,刷PTA的朋友都认识一下柳神–PTA解法大佬

本文由参考于柳神博客写成

柳神的CSDN博客,这个可以搜索文章

柳神的个人博客,这个没有广告,但是不能搜索

还有就是非常非常有用的 算法笔记 全名是

算法笔记  上级训练实战指南		//这本都是PTA的题解
算法笔记

PS 今天也要加油鸭

在这里插入图片描述

题目原文

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

生词如下:

Mirror 镜子

PS:题目也没有讲,这个序列就是插入序列啊.

吐了.还是看算法笔记知道的

思路如下:

① 就是建立一颗正常的树,然后再建立一颗镜像树.

② 再建立几个序列,先序输出的,镜像的先序输出的.

后序输出的,镜像的后序输出的.

然后再比较久OK了

PS:我是真的呆了,一直写错,感觉还不是很熟练.理解的还不是很透彻

有空要去LeetCode上刷刷题

代码如下:

#include<iostream>
#include<vector>
using namespace std;
struct node {
	int data;				//数据域
	node* left,* right;		//指针域
};
void insert(node*& root, int data) {					//插入函数
	if (root == NULL) {
		root = new node;
		root->data = data;
		root->left = root->right=NULL;
		return;
	}
	if (data < root->data)	insert(root->left, data);
	else insert(root->right, data);
}
//先序遍历,结果存放再vi中
void preOrder(node* root,vector<int> &vi) {
	if (root == NULL)	return;
	vi.push_back(root->data);
	preOrder(root->left, vi);
	preOrder(root->right, vi);
}
void preOrderMirror(node* root, vector<int>& vi) {
	if (root == NULL)	return;
	vi.push_back(root->data);
	preOrderMirror(root->right, vi);
	preOrderMirror(root->left, vi);
}
void postOrder(node* root, vector<int>& vi) {
	if (root == NULL)	return;
	postOrder(root->left, vi);
	postOrder(root->right, vi);
	vi.push_back(root->data);
}
void postOrderMirror(node* root, vector<int>& vi) {
	if (root == NULL)	return;
	postOrderMirror(root->right, vi);
	postOrderMirror(root->left, vi);
	vi.push_back(root->data);
}
vector<int> origin, pre, preM, post, postM;
int main(void) {
	int n=0,t=0;
	
	node* root = NULL;				//这个就是头节点
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &t);
		origin.push_back(t);
		insert(root, t);
	}
	preOrder(root, pre);
	preOrderMirror(root, preM);
	postOrder(root, post);
	postOrderMirror(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;
}

柳神的思路:

分析:假设它是二叉搜索树,一开始isMirror为false,根据二叉搜索树的性质将已知的前序转换为后序,转换过程中,如果发现最后输出的后序数组长度不为n,那就设isMirror为true,然后清空后序数组,重新再转换一次(根据镜面二叉搜索树的性质),如果依旧转换后数组大小不等于n,就输出NO否则输出YES

柳神的代码如下:

#include <cstdio>
#include <vector>
using namespace std;
bool isMirror;
vector<int> pre, post;
void getpost(int root, int tail) {
    if(root > tail) return ;
    int i = root + 1, j = tail;
    if(!isMirror) {
        while(i <= tail && pre[root] > pre[i]) i++;
        while(j > root && pre[root] <= pre[j]) j--;
    } else {
        while(i <= tail && pre[root] <= pre[i]) i++;
        while(j > root && pre[root] > pre[j]) j--;
    }
    if(i - j != 1) return ;
    getpost(root + 1, j);
    getpost(i, tail);
    post.push_back(pre[root]);
}
int main() {
    int n;
    scanf("%d", &n);
    pre.resize(n);
    for(int i = 0; i < n; i++)
        scanf("%d", &pre[i]);
    getpost(0, n - 1);
    if(post.size() != n) {
        isMirror = true;
        post.clear();
        getpost(0, n - 1);
    }
    if(post.size() == n) {
        printf("YES\n%d", post[0]);
        for(int i = 1; i < n; i++)
            printf(" %d", post[i]);
    } else {
        printf("NO");
    }
    return 0;
}

模拟花了我半天时间(夸张)

条件是很巧妙的.

还有那个等于号也非常的巧妙 (就算有人和我讲这个思路,我边界条件想想都要想好久) 还是太弱了.

算法笔记的好理解.柳神的代码巧妙.

真的巧妙.

如果这篇文章对你有张帮助的话,可以用你高贵的小手给我点一个免费的赞吗

相信我,你也能变成光.

在这里插入图片描述

如果你有任何建议,或者是发现了我的错误,欢迎评论留言指出.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值