【PAT】1043 Is It a Binary Search Tree(25 分)
1043 Is It a Binary Search Tree(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:
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
分析:
1.对输入的一段序列生成一棵二叉搜索树
2.判断:
a.此二叉树的先序遍历和输入的序列相等,则输出此二叉树的后序遍历序列
b.在 a 不成立的情况下,求这棵二叉树的镜像先序遍历序列,若和输入的序列相等,则输出二叉树的镜像后序遍历序列
c.以上都不成立时,输出 "NO"
C++代码如下:
#include<iostream>
#include<vector>
using namespace std;
#define maxn 1005
struct Node {
int data;
Node *lchild=NULL, *rchild=NULL;
}; void insert(Node* &root, int x) {
if (root == NULL) {
root = new Node;
root->data = x;
return;
}
if (x >= root->data) {
insert(root->rchild, x);
}
else
insert(root->lchild, x);
return;
} Node* createBST(const vector<int> v) {
Node* root=NULL; for (int i = ; i < v.size(); i++) {
insert(root, v[i]);
}
return root;
} vector<int>pre, pre_mirr, post,post_mirr;
void preorder(Node*root) {
if (root == NULL) return;
pre.push_back(root->data);
preorder(root->lchild);
preorder(root->rchild);
} void mirrBST(Node*root) {
if (root == NULL)return;
pre_mirr.push_back(root->data);
mirrBST(root->rchild);
mirrBST(root->lchild);
} void postorder(Node*root) {
if (root == NULL)return;
postorder(root->lchild);
postorder(root->rchild);
post.push_back(root->data);
} void postmirrorder(Node*root) {
if (root == NULL)return;
postmirrorder(root->rchild);
postmirrorder(root->lchild);
post_mirr.push_back(root->data);
}
int main() {
int n;
int temp;
cin >> n;
vector<int>v;
for (int i = ; i < n; i++) {
cin >> temp;
v.push_back(temp);
}
Node*root = createBST(v);
preorder(root);
mirrBST(root);
if (v == pre) {
postorder(root);
cout <<"YES"<<endl<< post[];
for (int i = ; i < post.size(); i++)
cout <<' '<< post[i];
cout << endl;
}
else if (v == pre_mirr) {
postmirrorder(root);
cout << "YES" << endl << post_mirr[];
for (int i = ; i < post_mirr.size(); i++)
cout << ' ' << post_mirr[i];
cout << endl;
}
else cout << "NO" << endl;
return ;
}
【PAT】1043 Is It a Binary Search Tree(25 分)的更多相关文章
- PAT 1043 Is It a Binary Search Tree (25分) 由前序遍历得到二叉搜索树的后序遍历
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- PAT 甲级 1043 Is It a Binary Search Tree (25 分)(链表建树前序后序遍历)*不会用链表建树 *看不懂题
1043 Is It a Binary Search Tree (25 分) A Binary Search Tree (BST) is recursively defined as a bina ...
- 1043 Is It a Binary Search Tree (25分)(树的插入)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 【PAT甲级】1043 Is It a Binary Search Tree (25 分)(判断是否为BST的先序遍历并输出后序遍历)
题意: 输入一个正整数N(<=1000),接下来输入N个点的序号.如果刚才输入的序列是一颗二叉搜索树或它的镜像(中心翻转180°)的先序遍历,那么输出YES并输出它的后序遍历,否则输出NO. t ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- PAT 1043 Is It a Binary Search Tree[二叉树][难]
1043 Is It a Binary Search Tree(25 分) A Binary Search Tree (BST) is recursively defined as a binary ...
- PAT Advanced 1043 Is It a Binary Search Tree (25) [⼆叉查找树BST]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- PAT (Advanced Level) 1043. Is It a Binary Search Tree (25)
简单题.构造出二叉搜索树,然后check一下. #include<stdio.h> #include<algorithm> using namespace std; +; st ...
- PAT甲题题解-1043. Is It a Binary Search Tree (25)-二叉搜索树
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789220.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT 1043 Is It a Binary Search Tree
#include <cstdio> #include <climits> #include <cstdlib> #include <vector> co ...
随机推荐
- 【题解】 Codeforces 919F A Game With Numbers(拓扑排序+博弈论+哈希)
懒得复制,戳我戳我 Solution: 我感觉我也说不太好,看Awson的题解吧. 说一点之前打错的地方: 连边存的是hash后的数组下标 if(ans[ num( C[a.hash()] , C[b ...
- 【POJ3171】Cleaning Shifts 带权区间最小覆盖
题目大意:给定一个长度为 N 的序列,求带权区间最小覆盖. 题解:设 \(dp[i]\) 表示从左端点到 i 的最小权值是多少,则状态转移为:\(dp[e[i].ed]=min\{dp[j],j\in ...
- @RequestParam注解一般用法
原文链接:https://www.cnblogs.com/likaileek/p/7218252.html SpringMVC注解@RequestParam全面解析 在此之前,写项目一直用的是@R ...
- jQuery获取radio选中后的文字
原文链接:http://blog.csdn.net/zhanyouwen/article/details/51393216 jQuery获取radio选中后的文字转载 2016年05月13日 10:3 ...
- angular模块
深入浅析AngularJS中的模块 模块是AngularJS应用程序的一个组成部分,模块可以是一个Controller.Service服务.Filter过滤器.directive指令,这些都属于模块. ...
- haproxy acl访问限制IP
http-request: 7层过滤 tcp-request content: tcp层过滤,四层过滤 注意 四层 采用 accept和reject 七层采用 allow和deny ...
- SHELL (3) —— 变量知识进阶和实践
摘自:Oldboy Linux运维——SHELL编程实战 SHELL中特殊切重要的变量 位置变量 作用说明 $0 获取当前执行的Shell脚本的文件名,如果执行脚本包含了路径,那么就包括脚本路径 $n ...
- H5 以及 CSS3
<!DOCTYPE html> <html> <head> <style> *{ padding:0; margin:0; } header{ disp ...
- html5 canvas loading(这可怕的编辑器,自动把我的canvas转义了)---以前收藏的整理了一下
/* super inefficient right now, could be improved */ var c = document.getElementById('canvasload'), ...
- Web性能优化系列(2):剖析页面绘制时间
本文由 伯乐在线 - J.c 翻译,sunbiaobiao 校稿.未经许可,禁止转载!英文出处:www.deanhume.com.欢迎加入翻译小组. 最近,我参加了在伦敦举办的Facebook移动开发 ...