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 分)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 【PAT甲级】1043 Is It a Binary Search Tree (25 分)(判断是否为BST的先序遍历并输出后序遍历)

    题意: 输入一个正整数N(<=1000),接下来输入N个点的序号.如果刚才输入的序列是一颗二叉搜索树或它的镜像(中心翻转180°)的先序遍历,那么输出YES并输出它的后序遍历,否则输出NO. t ...

  5. PAT甲级:1064 Complete Binary Search Tree (30分)

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

  6. 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 ...

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

  8. PAT (Advanced Level) 1043. Is It a Binary Search Tree (25)

    简单题.构造出二叉搜索树,然后check一下. #include<stdio.h> #include<algorithm> using namespace std; +; st ...

  9. PAT甲题题解-1043. Is It a Binary Search Tree (25)-二叉搜索树

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789220.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  10. PAT 1043 Is It a Binary Search Tree

    #include <cstdio> #include <climits> #include <cstdlib> #include <vector> co ...

随机推荐

  1. BZOJ2423 HAOI2010最长公共子序列(动态规划)

    大讨论.注意去重. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib& ...

  2. MT【90】图论基础知识及相关例题

    此讲适合参加全国联赛二试的同学 介绍图论和我们学习的一般的知识点比如函数一样,首先要介绍一些定义,只是图论里的定义相对较多,这里给出部分在竞赛中常用到的: 就像学函数的时候,学了定义和相关概念后我们要 ...

  3. 【刷题】洛谷 P4782 【模板】2-SAT 问题

    题目背景 2-SAT 问题 模板 题目描述 有n个布尔变量 \(x_1\)​~\(x_n\)​,另有m个需要满足的条件,每个条件的形式都是"\(x_i\)​为true/false或\(x_j ...

  4. HGOI20190126 模拟赛

    /* 最后一题比较难! */ solution:观察这个奇怪的图,不能共用走廊,就是1.2打包,3,4打包,每个包之间连线的线段覆盖问题. 考虑吧每个数映射成一个约为一半的数,且相邻(前奇后偶映射值一 ...

  5. 洛谷 P2178 [NOI2015]品酒大会 解题报告

    P2178 [NOI2015]品酒大会 题目描述 一年一度的"幻影阁夏日品酒大会"隆重开幕了.大会包含品尝和趣味挑战 两个环节,分别向优胜者颁发"首席品酒家"和 ...

  6. 洛谷 P2224 [HNOI2001]产品加工 解题报告

    P2224 [HNOI2001]产品加工 题目描述 某加工厂有A.B两台机器,来加工的产品可以由其中任何一台机器完成,或者两台机器共同完成.由于受到机器性能和产品特性的限制,不同的机器加工同一产品所需 ...

  7. mes平台的一些方法

    1.打开的一个缓存的页面的代码 $.openPane({ "width":"1500px",     "height":"1000 ...

  8. 界面编程之QT的数据库操作20180801

    /*******************************************************************************************/ 一.数据库连 ...

  9. jq禁用html标签

    原文:http://www.jb51.net/article/105154.htm 移除或禁用html元素的点击事件可以通过css实现也可以通过js或jQuery实现. 一.CSS方法 .disabl ...

  10. java基础-网络编程(Socket)技术选型入门之NIO技术

    java基础-网络编程(Socket)技术选型入门之NIO技术 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.传统的网络编程 1>.编写socket通信的MyServer ...