博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT Advanced Level 1043
阅读量:4541 次
发布时间:2019-06-08

本文共 3533 字,大约阅读时间需要 11 分钟。

1043 Is It a Binary Search Tree (25)(25 分)

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 这题也是血汗泪啊!前前后后鼓捣两个小时,其实不至于,看了题解的思路其实很好实现。 但是我纠结在我的思路里出不去了,我就想把它实现出来,最后终于折腾出来了。 其实 我的思路也很简洁,但是有一个小坑我找了好久。 具体思路是:   由于二叉排序树的中序遍历是有序的,那么二叉排序树和它的镜像树的中序遍历序列 其实就是已知序列排个序,从小到大排一下,从大到小排一下,用得到的中序序列和已知 的先序序列建树,不能建成功就说明不是。PAT 1020也用到了中序和先序序列建树的算法。 啊!赶紧去做数学了,今天刷题用的时间太多了,任务要完不成了,祝我圆满完成任务。
/**********************author: yomidate: 18.8.17ps:**********************/#include 
#include
using namespace std;struct node{ int data; node* lchild; node* rchild;};int pre[1500];int post[1500];int in[1500];int flag = 1, cnt = 0, flag1 = 0, t = 0;int cmp(int a, int b){ return a>b;}void post_order(node* root){ if(root!=NULL){ post_order(root->lchild); post_order(root->rchild); post[cnt++] = root->data; return; }}node* create(int preL, int preR, int inL, int inR){// if(flag == 0)// return NULL; if(preL>preR){ return NULL; } int k = -1; if(flag1){ for(int i=inR; i>=inL; i--){///坑--->我找了好久 if(in[i] == pre[preL]){ k = i; break; } } } else{ for(int i=inL; i<=inR; i++){ if(in[i] == pre[preL]){ k = i; break; } } } if(k == -1) return NULL; int numLeft = k-inL; node* root = new node; root->data = pre[preL]; root->lchild = create(preL+1, preL+numLeft, inL, k-1); root->rchild = create(preL+numLeft+1, preR, k+1, inR); t++; return root;}int main(){ int n; cin >> n; flag = 1; node* root; for(int i=1; i<=n; i++){ cin >> pre[i]; in[i] = pre[i]; } sort(in+1, in+n+1); root = create(1, n, 1, n); if(t != n){ flag1 = 1; t = 0; sort(in+1, in+n+1, cmp); root = create(1, n, 1, n); //cout << t << endl; } if(t != n) cout << "NO"; else{ cout << "YES" << endl; // cout << root->data << endl; post_order(root); for(int i=0; i

 

转载于:https://www.cnblogs.com/AbsolutelyPerfect/p/9494972.html

你可能感兴趣的文章
python单元测试框架pytest——fixture函数(类似unitest的setup和teardown)
查看>>
Hadoop源码分析8: IPC流程(3)客户端的clients、connections、calls复用
查看>>
[MVC]View
查看>>
Django REST FRAMEWORK swagger(二)model序列化
查看>>
一点随想
查看>>
SVN操作步骤
查看>>
Xianqi UVa 1589
查看>>
无刷新效果统计在线人数
查看>>
2017-2018-2 1723《程序设计与数据结构》问题汇总 (更新完毕)
查看>>
c# 通过反射 实例化类
查看>>
[ubuntu]中文用户目录路径改英文
查看>>
spark 编程教程
查看>>
LeetCode--Valid Parentheses
查看>>
BZOJ3124 SDOI2013 直径 DFS
查看>>
BZOJ4566: [Haoi2016]找相同字符
查看>>
python:extend (扩展) 与 append (追加) 之间的天与地
查看>>
Python测试——安装篇总结
查看>>
7 -- Spring的基本用法 -- 11... 基于XML Schema的简化配置方式
查看>>
输入1则输出0,输入0则输出1
查看>>
placeholder字体样式及兼容
查看>>