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. BZOJ2741 FOTILE模拟赛L(分块+可持久化trie)

    显然做个前缀和之后变成询问区间内两个数异或最大值. 一种暴力做法是建好可持久化trie后直接枚举其中一个数查询,复杂度O(nmlogv). 观察到数据范围很微妙.考虑瞎分块. 设f[i][j]为第i个 ...

  2. Berland and the Shortest Paths CodeForces - 1005F(最短路树)

    最短路树就是用bfs走一遍就可以了 d[v] = d[u] + 1 表示v是u的前驱边 然后遍历每个结点 存下它的前驱边 再用dfs遍历每个结点 依次取每个结点的某个前驱边即可 #include &l ...

  3. Oil Skimming HDU - 4185(匹配板题)

    Oil Skimming Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  4. java实现超大整数加减乘除四则运算

    原理: 用数组存储数字,按照计算法则进行运算. 代码: package com.hdwang; import java.util.regex.Matcher; import java.util.reg ...

  5. spring data jpa查询部分字段、多余附加字段

    spring data jpa查询部分字段 第一种方法:使用 model 查询时转化 首先建立一个 model ,写上自己想要查询的字段,然后写上构造函数,这步很重要,因为spring jpa 转化时 ...

  6. Linux上防火墙开放对应的端口

    在Linux上防火墙开放对应的端口的命令如下: 方式一: [root@localhost sbin]# /sbin/iptables -I INPUT -p tcp --dport 80 -j ACC ...

  7. Redis的持久化数据

    Redis支持两种持久化:RDB和AOF模式 一.名词解释: RDB:持久化可以在指定的时间间隔内生成数据集的时间点快照(point-in-time snapshot).AOF:持久化记录服务器执行的 ...

  8. web开发中的跨域整理

    1.springboot通过CROS实现跨域: https://www.cnblogs.com/520playboy/p/7306008.html springboot下各种跨域方式: http:// ...

  9. 生成ssh-key for GIthub

    在Github里,如果我们想通过ssh的方式进行身份验证,我们就需要建立ssh-key: 方法一: git GUI,点击help,选择Generate ssh key

  10. Hadoop生态圈-Flume的组件之sink处理器

    Hadoop生态圈-Flume的组件之sink处理器 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一. 二.