PAT 甲级 1043 Is It a Binary Search Tree (二叉排序树)(c++版)(附代码注释)

1043 Is It a Binary Search Tree (25 分)

原文链接:http://kakazai.cn/index.php/Kaka/Pat/query/id/133

题目

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805440976633856

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)左子树的所有结点的值都小于根结点的值;2)右子树的所有结点的值都大于或等于根结点的值;3)左右子树也是一棵二叉排序树

MBST:交换BST的左右孩子,可以得到它的Mirror Image

给出一个前序遍历,判断是否BST或MBST的先序遍历。若是,则给出BST或MBST的后序遍历。

  • 分析

(1)先判断是否BST的先序。

BST的先序是【根、左、右】,且 左 < 右。若能在根结点后面找到一个分界处,让前区间的元素全部小于后区间的元素,且对前后区间递归判断也是这结果,则该树是BST。否则不是BST。

(2)再判断是否MBST的先序。

MBST的先序是【根、右、左】,且 左 < 右。若能在根结点后面找到一个分界处,让前区间的元素全部大于后区间的元素,且对前后区间递归判断也是这结果,则该树是MBST。否则不是MBST。

(3)若是上面的其中一个,则输出其后序遍历

(4)若上面两个都不是,则输出NO

知识点与坑点

  • 知识点

1)二叉排序树的先序遍历、后序遍历

  • 坑点

1)

一、定义法

算法思路

1 先判断是否BST的先序

2 再判断是否MBST的先序

3 若是上面的其中一个,则输出其后序遍历;若上面两个都不是,则输出NO

代码-c++版
#include<vector>
using namespace std;
const int maxn = 1001;	//最多有1000个结点

/* 关键变量 */ 
int n;
int pre[maxn];	//先序遍历
vector<int> bst_post,mbst_post;	//bst的后序,mbst的后序 

/* 递归判断某区间是否符合BST的先序区间特点*/ 
int bst = 1;	//假设该树是BST 
void buildbst(int prel, int prer) {	  //先序区间[prel,prer]
  if (prel > prer)return;             //区间不合法,结束
  
  int root = pre[prel];	//根结点 
  
  //从根结点后开始,寻找左子树        
  int k = prel + 1;					  
  while (k <= prer && pre[k] < root)k++; 
  buildbst(prel + 1,k - 1); //递归判断左子树 
  
  //从左子树后面,寻找右子树 
  int temp = k; 
  while (k <= prer && pre[k] >= root)k++;
   
   //判断是否合法
  if(k < prer+1){	//右子树后面还有元素,则不是BST,结束 
  	bst = 0;
    return;
  }else{	//k=prer+1,即右子树后面没元素了,合法 
  	buildbst(temp, prer);	//递归判断右子树 
  }
    
//后序,根结点入队 
  bst_post.push_back(root);	
}

/* 递归判断某区间是否符合MBST的先序区间特点*/ 
int mbst = 1;	//假设该树是MBST 
void buildmbst(int prel, int prer) {	  //先序区间[prel,prer]
  if (prel > prer)return;             //区间不合法,结束
  
  int root = pre[prel];	//根结点 
  
  //从根结点后开始,寻找右子树        
  int k = prel + 1;					  
  while (k <= prer && pre[k] >= root)k++; 
  buildmbst(prel + 1,k - 1); //递归判断右子树 
  
  //从右子树后面,寻找左子树 
  int temp = k; 
  while (k <= prer && pre[k] < root)k++; 
  if(k < prer+1){	//左子树后面还有元素,则不是MBST,结束 
  	mbst = 0;
    return;
  }else{	//k=prer+1,即左子树后面没元素了,合法 
  	buildmbst(temp, prer);	//递归判断左子树 
  }
    
  //后序,根结点入队
  mbst_post.push_back(root);	 
}

//打印后序 
void print(vector<int> post){
	printf("%d", post[0]);
    for (int i = 1; i < post.size(); i++){
    	printf(" %d", post[i]);
	}
}


int main() {
	
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {	//接收先序,下标从1开始 
    scanf("%d", &pre[i]);
  }
  
  buildbst(1, n);	//判断是否BST的先序 
  buildmbst(1, n);	//判断是否MBST的先序 
  if (bst == 0 && mbst == 0) printf("NO\n");	//两个都不是 
  else {
    printf("YES\n");
    if (bst == 1) {	//是BST 
    	print(bst_post); 
    }
    else {	//是MBST 
    	print(mbst_post);
    } 
  } 
  return 0;
}
代码-python版

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值