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. AAC 格式分析

    一直在做一个语音项目,到了测试阶段,近来不是很忙,想把之前做的内容整理一下. 关于AAC音频格式基本情况,可参考维基百科http://en.wikipedia.org/wiki/Advanced_Au ...

  2. 《Genesis-3D开源游戏引擎完整实例教程-跑酷游戏篇:简介及目录》(附上完整工程文件)

    跑酷游戏制作 游戏类型: 此游戏Demo,为跑酷类游戏. 框架简介: 游戏通常由程序代码和资源组成.如果说模型.贴图.声音之类的可以给游戏环境提供一个物理描述和设置,那么脚本和代码块会给游戏赋予生命, ...

  3. C#中的堆和栈

    看到一篇讲堆和栈的文章,是我目前为止见到讲的最易懂,详细和深入的.我翻译成中文.以此总结. 原文=>C#Heap(ing) Vs Stack(ing) in .NET 在net framewor ...

  4. linux vim用法总结

    1.跳转到指定行 编辑模式下:输入  ngg或nG(n代表行数) 命令模式下:输入  :n(n代表行数) 2.查找命令 命令模式下输入 / 后面加上查找的内容 例如    :/name     (查找 ...

  5. NetAddr

    http://www.searchdatabase.com.cn/showcontent_66349.htm   [techTarget中国,其专注于IT领域企业级高端市场,为IT专业技术人员和管理决 ...

  6. Struts2 文件上传

    一:表单准备 ① 要想使用HTML 表单上传一个或多个文件     –须把 HTML表单的 enctype属性设置为multipart/form-data     –须把HTML 表单的method ...

  7. jpa仓库接口

    可以使用的仓库接口有: Repository: 是 Spring Data的一个核心接口,它不提供任何方法,开发者需要在自己定义的接口中声明需要的方法. CrudRepository: 继承Repos ...

  8. 对于一个网站,如何禁止直接从浏览器Web browser中访问js文件

    比如有一个网站,https://testsystem.infotest.com 在这个网站的内容文件目录下面,有一个scripts文件夹,该文件夹中有一个js文件,比如lukeTest.js文件 这样 ...

  9. PAC(Proxy Auto Config)代理自动配置文件的编写

    Proxy Auto Config文件格式说明 PAC文件实际上是一个Script, 通过PAC我们可以让系统根据情况判断使用哪一个Proxy来访问目标网址, 这样做的好处: 分散Proxy的流量,避 ...

  10. Netty笔记

    1 基本介绍 Bootstrap Netty应用程序通过设置 bootstrap(引导)类开始,该类提供了一个用于应用程序网络层配置的容器.Bootstrap有两种类型,一种是用于客户端的Bootst ...