the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1043

and the source code is as followed.

#include<iostream>
#include<vector>
#include<cstdio> using namespace std;
int pos; struct cell
{
cell *lchild;
cell *rchild;
int c;
cell()
{
lchild = rchild = NULL;
}
}; void insert1(cell *&root,int x)
{
if (!root)
{
root = new cell;
root->c = x;
}
else
{
if (x < root->c)
{
insert1(root->lchild,x);
}
else
insert1(root->rchild,x);
}
}
void insert2(cell *&root,int x)
{
if (!root)
{
root = new cell;
root->c = x;
}
else
if (x > root->c)
{
insert2(root->lchild, x);
}
else
insert2(root->rchild, x);
}
void preOrder(cell *root,vector<int> &v)
{
v.push_back(root->c);
if (root->lchild != NULL)
preOrder(root->lchild,v);
if (root->rchild != NULL)
preOrder(root->rchild,v);
} void postOrder(cell *root,vector<int> &v)
{
if (root->lchild != NULL)
postOrder(root->lchild, v);
if (root->rchild != NULL)
postOrder(root->rchild, v);
v.push_back(root->c);
} int main()
{
int n;
vector<int> v1,v2,v3,v;
scanf("%d",&n);
for (int i = 0; i < n; i++)
{
int x;
scanf("%d",&x);
v1.push_back(x);
}
cell *r = NULL;//THE root of bst
cell *r1 = NULL;//the root of mirror of bst;
for (int i = 0; i < n; i++)
{
insert1(r,v1[i]);
insert2(r1,v1[i]);
}
preOrder(r,v2);//the preorder of bst
preOrder(r1,v3);//the preorder of the mirror of bst if (v2 != v1 && v3 != v1)
{
printf("NO\n");
return 0;
}
else
{
printf("YES\n");
if (v2 == v1)
{
postOrder(r,v);
for (int i = 0; i < n; i++)
{
printf("%d%c",v[i], ((i - n + 1) ? ' ' : '\n'));//this type of presentation is fitted for the pattern of pat. }
}
else if(v3 == v1)
{
postOrder(r1,v);
for (int i = 0; i < n; i++)
printf("%d%c", v[i], ((i - n + 1) ? ' ' : '\n'));
}
}
return 0;
}

i think what is the most important for the bst-like problem is build a bst. that’s the point.

as long as the bst is built,then some trial things remains.

1043. Is It a Binary Search Tree (25)的更多相关文章

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

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

  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分) 由前序遍历得到二叉搜索树的后序遍历

    题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...

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

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

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

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

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

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

  8. pat1043. Is It a Binary Search Tree (25)

    1043. Is It a Binary Search Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

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

随机推荐

  1. WinForm使用webbrowser爬取数据,中文乱码问题

    使用winform中的webbrowser抓取某个网站的数据时,遇到中文乱码的问题: 当时使用的获取网页内容的代码为: webBrowser1.DocumentText.ToString(); 不管我 ...

  2. 一道JAVA经典面试题目的两种解法

    题目要求:String s="-1 2 5 78 129 -65 -23";将字符串进行升序排序后输出. 方法一:使用数组进行排序 思路: 1.获取字符串中的数值:   2.将数组 ...

  3. [LeetCode] Remove Element 分析

    Remove Element算是LeetCode的一道水题,不过这题也有多种做法,现就我所知的几种做一点讨论. 题目链接:https://leetcode.com/problems/remove-el ...

  4. angular form-data文件上传

    前言:很久没更新博客,最近公司pc端技术选型用angular,这几天就赶鸭子上架,硬着头皮直接上手angular.其中有许多小坑陆陆续续踩起走.今天就遇到一个比较常见的问题:图片上传. 主题:图片上传 ...

  5. 如何在 Windows Azure 的虚拟机 ubuntu 上面安装和配置 openVPN(三)

    第三步:安装openVPN 请打开 ubuntu 官方文档.然后,从上到下,按照步骤,输入执行命令.大家只需要执行到 First trouble shooting 之前即可. 不要怕麻烦,仔细读,一步 ...

  6. C++ 我想这样用(四)

    嗯,已经是第四篇了,这篇起我就要开始细说语法了,不过在那之前再次申明下主旨:"C++我想这样用" 系列文案是为C程序员打造的,不是C++程序员. 我的终极目标是:让那些觊觎面向对象 ...

  7. FindBugs Bug Descriptions

    FindBugs Bug Descriptions ◇例1: Integer a = ; String str ="; System.out.println(str == a.toStrin ...

  8. 北京Uber优步司机奖励政策(3月5日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  9. homework -06 围棋

    playPrev功能的实现 public void playPrev(ref GoMove gm) { Point p = gm.Point; m_colorToPlay = gm.Color; cl ...

  10. [iOS 多线程 & 网络 - 2.10] - ASI框架下载文件

    A.ASI框架中的下载 1.实现步骤 在实际的开发中如果要使用asi框架来下载服务器上的文件,只需要执行下面简单的几个步骤即可. (1)创建请求对象:(2)设置下载文件保存的路径:(3)发送下载文件的 ...