1043. Is It a Binary Search Tree (25)
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)的更多相关文章
- 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 ...
- 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 ...
- 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分) 由前序遍历得到二叉搜索树的后序遍历
题目 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 (25 分)(判断是否为BST的先序遍历并输出后序遍历)
题意: 输入一个正整数N(<=1000),接下来输入N个点的序号.如果刚才输入的序列是一颗二叉搜索树或它的镜像(中心翻转180°)的先序遍历,那么输出YES并输出它的后序遍历,否则输出NO. t ...
- pat1043. Is It a Binary Search Tree (25)
1043. Is It a Binary Search Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- 【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 ...
随机推荐
- asp.net(class0625)
1 SiteMapPath 面包屑导航控件 要想使用这个控件,必须创建一个站点地图,也就是 web.sitemap web.sitemap是一个xml文件: 根节点必须是:<siteMap> ...
- Probabilistic SVM 与 Kernel Logistic Regression(KLR)
本篇讲的是SVM与logistic regression的关系. (一) SVM算法概论 首先我们从头梳理一下SVM(一般情况下,SVM指的是soft-margin SVM)这个算法. 这个算法要实现 ...
- 机器学习中的数学(4)-线性判别分析(LDA), 主成分分析(PCA)
版权声明: 本文由LeftNotEasy发布于http://leftnoteasy.cnblogs.com, 本文可以被全部的转载或者部分使用,但请注明出处,如果有问题,请联系wheeleast@gm ...
- 产品经理如何赢得开发人员的尊重和支持?-摘自infoq
对于产品经理来说,赢得开发人员的尊重和支持,从某种意义上讲,是产品迈向成功的坚实一步.最近,知乎社区上的开发人员和管理者在前.后两个帖子中对此展开了激烈的讨论,其中不乏真知灼见. 林志霖Cray认为产 ...
- 第二百九十七天 how can I 坚持
算是在家宅了一天吧,下午睡了会觉,晚上一起做了个饭,中午一起吃的炒菜和徐斌他同学. 还是那么冷啊. 整天都是东扯西扯的. 睡觉. 忘了件重要的事,就是今天第一次喝鸡尾酒,还有,常人之所以是常人,不是因 ...
- rsync 无密码传输文件
最近机器迁移,需要备份文件,但各个机器间不能穿梭,即无法通过scp来传输文件, 在运维的建议下,选用了rsync作为传输的工具. 默认情况Ubuntu安装了rsync服务,但在/etc下没有配置文件, ...
- UVALive 7457 Discrete Logarithm Problem (暴力枚举)
Discrete Logarithm Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/D Description ...
- POJ 1308 Is It A Tree? (并查集)
Is It A Tree? 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/M Description A tree is a w ...
- [iOS 多线程 & 网络 - 3.0] - 在线动画Demo
A.需求 所有数据都从服务器下载 动画列表包含:图片.动画名标题.时长副标题 点击打开动画观看 code source: https://github.com/hellovoidworld/Vid ...
- POJ 1151 Atlantis (扫描线+线段树)
题目链接:http://poj.org/problem?id=1151 题意是平面上给你n个矩形,让你求矩形的面积并. 首先学一下什么是扫描线:http://www.cnblogs.com/scau2 ...