【c/c++】PAT A1043 Is It a Binary Search Tree

Question: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.

即,通过前序序列判断,所给前序是否为二叉搜索树或其镜像树的前序序列。

思路:如果所给前序是二叉搜索树或其镜像的前序,那么按照二叉搜索树的构建方法,生成的二叉搜索树,再得到其前序序列或者其镜像的前序序列,其中之一的结果应该与输入数据每一位都相同,因此可以凭借这个进行解题。代码如下:

#include<stdio.h>
#include<vector>
using namespace std;

const int maxn = 1010;
vector<int> pre, preM, post, postM, t;

struct node{
	int data;
	node* lchild;
	node* rchild;
} Node[maxn]; 

node* newNode(int data){    //创建新的结点
	node* Node = new node;
	Node->data = data;
	Node->lchild = NULL;
	Node->rchild = NULL;
	return Node;
}

void insert(node* &root, int data){   //按照二叉搜索树的定义构建二叉搜索树
	if(root == NULL){
		root = newNode(data);
		return;
	}
	if(data < root->data){
		insert(root->lchild, data);
	}else{
		insert(root->rchild, data);
	}
}

void preOrder(node* root){       //得到二叉搜索树的前序序列
	if(root == NULL) return;
	pre.push_back(root->data);
	preOrder(root->lchild);
	preOrder(root->rchild);
}

void preOrderM(node* root){    //得到二叉搜索树的镜像的前序序列
	if(root == NULL) return;
	preM.push_back(root->data);
	preOrderM(root->rchild);
	preOrderM(root->lchild);
}

void postOrder(node* root){    //得到二叉搜索树的后序序列
	if(root == NULL) return;
	postOrder(root->lchild);	
	postOrder(root->rchild);
	post.push_back(root->data);
}

void postOrderM(node* root){      //得到二叉搜索树镜像的后序序列
	if(root == NULL) return;
	postOrderM(root->rchild);	
	postOrderM(root->lchild);
	postM.push_back(root->data);
}

int main(){
	int N;
	int num;
	node* root = NULL;
	scanf("%d", &N);
	for(int i=0;i<N;i++){
		scanf("%d", &num);
		t.push_back(num);
		insert(root, num);     //构建二叉搜索树
	}
//	for(int i=0;i<N;i++){
//		printf("%d ", t[i]);
//		if(i<N-1) printf(" ");
//	}
	preOrder(root);  
	preOrderM(root);
	if(pre==t){       //如果输入与二叉搜索树的前序相同
		postOrder(root);
		printf("YES\n");
		for(int i=0;i<N;i++){
			printf("%d", post[i]);
			if(i<N-1) printf(" ");
		}
	}else if(preM==t){  //如果输入与二叉搜索树的镜像的前序相同
		postOrderM(root);
		printf("YES\n");
		for(int i=0;i<N;i++){
			printf("%d", postM[i]);
			if(i<N-1) printf(" ");
		}
	}else{
		printf("NO");
	}

	return 0;
}

不使用vector的代码:

#include<stdio.h>
using namespace std;
int N;
const int maxn = 1010;
int dat[maxn] = {0};
int dat1[maxn] = {0};
int dat2[maxn] = {0};
int t = 0;
bool flag = true;
bool flag1 = true;
struct node{
	int data;
	node* lchild;
	node* rchild;
};
node* newNode(int a){
	node* Node = new node;
	Node->data = a;
	Node->lchild = Node->rchild = NULL;
	return Node;
}
void insert(node* &root, int a){
	if(root==NULL){
		root = newNode(a);
		return;
	}
	if(a>=root->data) insert(root->rchild,a);
	if(a<root->data) insert(root->lchild,a);
}
node* Create(){
	node* root = NULL;
	for(int i=0;i<N;i++){
		insert(root,dat[i]);
//		printf("%d\n",dat[i]);
	}
	return root;
}
void preOrder(node* root){   //得到二叉搜索树的前序序列
	if(root==NULL) return;
	dat1[t++] = root->data;
	preOrder(root->lchild);
	preOrder(root->rchild);
}
void preOrderM(node* root){    //得到二叉搜索树的镜像的前序序列
	if(root==NULL) return;
	dat2[t++] = root->data;
	preOrderM(root->rchild);
	preOrderM(root->lchild);
}
void postOrder(node* root){    //得到二叉搜索树的后序序列
	if(root==NULL) return;
	postOrder(root->lchild);
	postOrder(root->rchild);
	printf("%d",root->data);
	t++;
	if(t<N) printf(" ");
}
void postOrderM(node* root){      //得到二叉搜索树的镜像的后序序列
	if(root==NULL) return;
	postOrderM(root->rchild);
	postOrderM(root->lchild);
	printf("%d",root->data);
	t++;
	if(t<N) printf(" ");
}
void judge(){
	for(int i=0;i<N;i++){
		if(dat[i]!=dat1[i]){
			flag = false;
			break;
		}
	}
	for(int i=0;i<N;i++){
		if(dat[i]!=dat2[i]){
			flag1 = false;
			break;
		}
	}
}
int main(){
	scanf("%d",&N);
	for(int i=0;i<N;i++){
		scanf("%d",&dat[i]);
	}
	node* root = Create();
	preOrder(root);
	t = 0;
	preOrderM(root);
	judge();
	if(flag){
		printf("YES\n");
		t = 0;
		postOrder(root);
	}else if(flag1){
		printf("YES\n");
		t = 0;
		postOrderM(root);
	}else{
		printf("NO");
	} 
	return 0;
}

AC截图:
在这里插入图片描述
运行截图:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值