PAT (Advanced Level) 1043. Is It a Binary Search Tree (25) 判断序列是否为BST的先序遍历,递归

来源:互联网 发布:矩阵控制系统 编辑:程序博客网 时间:2024/06/15 18:58

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:
78 6 5 7 10 8 11
Sample Output 1:
YES5 7 6 8 11 10 8
Sample Input 2:
78 10 11 8 6 7 5
Sample Output 2:
YES11 8 10 7 5 6 8
Sample Input 3:
78 6 8 5 10 9 11
Sample Output 3:
NO
没必要建树,使用递归写即可,看能否按给定元素顺序用完所有元素建立起BST或BST的镜像。按照根、右、左的顺序处理,这样逆序输出结果就是左、右、根的后序遍历。
以序列本身为BST先根遍历为例,如下面的函数f1,用vector<int> &result存储结果。序列第一个元素为根,然后是都比根小的左子树的元素,最后是都比根大的右子树的元素,若能把所有元素用完,返回true。递归时先将根节点元素压入结果数组,对右子树进行递归,再对左子树递归,途中若发现一个子树返回false,说明不能把元素用完,直接输出NO,结束程序。若所有递归函数返回true,说明用完了所有元素,逆序输出result即为左、右、根的后序遍历。
检测序列为BST镜像的先序遍历,如下面的函数f2,代码与f1几乎一样,只有左右子树元素在与根结点比较大小时与f1相反。
/*2015.7.25cyq*/#include <iostream>#include <vector>using namespace std;vector<int> data;//检测序列是不是BST,当不能把begin到end之间的所有元素用完,返回false//按照根、右、左的顺序进行处理,这样只要把结果逆序输出就是后序遍历bool f1(const vector<int> &a,int begin,int end,vector<int> &result){if(begin>end)//说明该子树不存在,直接返回return true;result.push_back(a[begin]);if(begin==end)return true;int mid=begin+1;while(mid<=end&&a[begin]>a[mid])//a[mid]为第一个不比a[begin]小的mid++;if(mid==end+1){//无右子树,直接检测左子树能不能用完元素if(!f1(a,begin+1,end,result))return false;}else{//a[mid]及之后都应该大于等于a[begin]int right=mid;while(right<=end&&a[begin]<=a[right])right++;if(right<=end)//检查出右子树存在比a[begin]小的return false;else{if(!f1(a,mid,end,result))//先处理右子树return false;if(!f1(a,begin+1,mid-1,result))return false;return true;}}}//检测镜像是不是BST,代码跟f1类似,只有与a[begin]比较大小的时候相反,同样按照根、右、左进行处理bool f2(const vector<int> &a,int begin,int end,vector<int> &result){if(begin>end)return true;result.push_back(a[begin]);if(begin==end)return true;int mid=begin+1;while(mid<=end&&a[begin]<=a[mid])mid++;if(mid==end+1){//无右子树if(!f2(a,begin+1,end,result))return false;}else{//a[mid]及之后都应该小于a[begin]int right=mid;while(right<=end&&a[begin]>a[right])right++;if(right<=end)//检查出右子树存在比a[begin]大的return false;else{if(!f2(a,mid,end,result))//先处理右子树return false;if(!f2(a,begin+1,mid-1,result))return false;return true;}}}int main(){int N;cin>>N;int x;for(int i=0;i<N;i++){cin>>x;data.push_back(x);}if(N==1){cout<<"YES"<<endl;cout<<data[0]<<endl;return 0;}vector<int> result;if(data[0]>data[1]){if(f1(data,0,N-1,result)){//序列是BST,逆序输出resultcout<<"YES"<<endl;cout<<result[N-1];for(int i=N-2;i>=0;i--)cout<<" "<<result[i];}elsecout<<"NO";}else{if(f2(data,0,N-1,result)){//镜像是BST,逆序输出resultcout<<"YES"<<endl;cout<<result[N-1];for(int i=N-2;i>=0;i--)cout<<" "<<result[i];}elsecout<<"NO";}return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 刚下的软件点开系统显示停用怎么办 红酒洋酒啤酒一起喝胃不舒服怎么办 儿子13岁初一不想读书了怎么办 微信不小心点了注册新账号怎么办 在京东买东西商家不发货怎么办 在京东买东西坏了商家不退货怎么办 苯扎氯铵溶液不小心喝了一口怎么办 苯扎氯铵溶液没有稀释就用了怎么办 牛油果切开了但是没熟怎么办 手机安装程序时解析包出错怎么办 因俩人不合适分手了很难受怎么办 中考结束后成绩不好的该怎么办 在京东自营药房买药没有处方怎么办 平安普惠账号不可以注销怎么办? 京东购物非自营货没到降价了怎么办 实体店商家不承认卖的是假货怎么办 衣服上的装饰圆扣掉下来了怎么办 没在京东买东西却收到退款怎么办 小米分期付款买的手机不要了怎么办 唯品会在线支付后商品有问题怎么办 红米手机把时间删了怎么办 红米桌面上的时间删了怎么办 华为手机玩游戏老是闪退怎么办 别人家无线网距离太远信号差怎么办 微信公众号交话费交错了怎么办 手机卡里还有话费销户的话怎么办 号码忘记交话费变成空号怎么办 多屏互动没办法隔空播放怎么办? 一个人长期受一件事的打击怎么办 物流信息显示快递被别人签收怎么办 现在打工的人被领导骂怎么办 加密狗丢了打不开软件了怎么办 手机微信可以打开网页打不开怎么办 手机中国网打开网速慢该怎么办 在韩国用中国软件网速特别慢怎么办 淘宝退货快递一直没显示到货怎么办 高二美术生集训时文化课怎么办 唯品会不支持7天无理由退货怎么办 sy来4am了孤存怎么办 淘宝网买的电器坏了怎么办 在闲鱼上买的电器是坏的怎么办