考察:

1.二叉树的建树

2.前序遍历,后序遍历

3.BST的特性

这题的思路:

告诉你数组是先序遍历的,so 根已经知道了(数组首位元素),那么按照BST,建一下树(要两次,另外一次是镜像的);

跑一跑先序遍历,对一对是不是呀?

然后是的话就输出后序遍历的方式。

THAT'S ALL;

#include <bits/stdc++.h>
using namespace std;
typedef long long LL; const int N=1e3+10; struct BST{
BST* Left;
BST* Right;
int w;
}; BST* Insert1(BST* p,int w)
{
if(p==NULL)
{
p=(BST*)malloc(sizeof(BST));
p->w=w;
p->Left=NULL;
p->Right=NULL;
return p;
}
if(w>=p->w)
p->Right=Insert1(p->Right,w);
else
p->Left=Insert1(p->Left,w);
return p;
} BST* Insert2(BST* p,int w)
{
if(p==NULL)
{
p=(BST*)malloc(sizeof(BST));
p->w=w;
p->Left=NULL;
p->Right=NULL;
return p;
}
if(w<p->w)
p->Right=Insert2(p->Right,w);
else
p->Left=Insert2(p->Left,w);
return p;
} vector<int>xs;
void Preorder(BST* p)
{
if(p==NULL) return;
xs.push_back(p->w);
Preorder(p->Left);
Preorder(p->Right);
} void Postorder(BST* p)
{
if(p==NULL) return;
Postorder(p->Left);
Postorder(p->Right);
xs.push_back(p->w);
} int main()
{
int n;
int a[N];
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
bool f1=true,f2=true;
BST* root;
root=(BST*)malloc(sizeof(BST));
root->w=a[1];
root->Left=NULL;
root->Right=NULL;
for(int i=2;i<=n;i++)
Insert1(root,a[i]);
xs.clear();
Preorder(root);
for(int i=0;i<n;i++)
if(a[i+1]!=xs[i])
{
f1=false;
break;
}
if(!f1)
{
root=(BST*)malloc(sizeof(BST));
root->w=a[1];
root->Left=NULL;
root->Right=NULL;
for(int i=2;i<=n;i++)
Insert2(root,a[i]);
xs.clear();
Preorder(root);
for(int i=0;i<n;i++)
if(a[i+1]!=xs[i])
{
f2=false;
break;
}
}
if(!f1&&!f2)
{
puts("NO");
return 0;
}
puts("YES");
xs.clear();
Postorder(root);
for(int i=0;i<n;i++)
{
if(i) printf(" ");
printf("%d",xs[i]);
}
return 0;
}

PAT 1043【BST与二叉树】的更多相关文章

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

  2. PAT归纳总结——关于二叉树的一些总结

    今天是6月26日到下个月的这个时候已经考过试了,为了让自己考一个更高的分数,所以我打算把PAT的相关题型做一个总结.目前想到的方法就是将相关的题型整理到一起然后,针对这种题型整理出一些方法. 二叉树的 ...

  3. LeetCode OJ:Kth Smallest Element in a BST(二叉树中第k个最小的元素)

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  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 L2-011 玩转二叉树

    https://pintia.cn/problem-sets/994805046380707840/problems/994805065406070784 给定一棵二叉树的中序遍历和前序遍历,请你先将 ...

  6. PAT L2-011 玩转二叉树(二叉树层序遍历)

    给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列.所谓镜面反转,是指将所有非叶结点的左右孩子对换.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出 ...

  7. PAT 1043 输出PATest(20)(代码+思路)

    1043 输出PATest(20)(20 分) 给定一个长度不超过10000的.仅由英文字母构成的字符串.请将字符重新调整顺序,按"PATestPATest...."这样的顺序输出 ...

  8. PAT 1020 Tree Traversals[二叉树遍历]

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  9. PAT——1043. 输出PATest

    给定一个长度不超过10000的.仅由英文字母构成的字符串.请将字符重新调整顺序,按“PATestPATest....”这样的顺序输出,并忽略其它字符.当然,六种字符的个数不一定是一样多的,若某种字符已 ...

随机推荐

  1. HDU 4034 Graph:反向floyd

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 题意: 有一个有向图,n个节点.给出两两节点之间的最短路长度,问你原图至少有多少条边. 如果无解 ...

  2. 一些有意思的面试题(持续更新) .C语言编程技巧札记

    一些有意思的面试题(持续更新) http://blog.csdn.net/wangyuling1234567890/article/details/38565239 C语言编程技巧札记 http:// ...

  3. Idea_学习_07_Idea常用配置

    二.参考资料 1.Android Studio 入门级教程(一)

  4. PHP 写入缓存

    1.创建file.PHP <?php class File{ //封装方法 private $_dir; const EXT='.text';//文件后缀,定义为常量 public functi ...

  5. nodejs stream基础知识

    分类 nodejs 的 stream 有四种: Readable:可读流 Writable: 可写流 Duplex:双工流 Transform:转换流 Readable // _read方法是从底层系 ...

  6. tensorflow中常量(constant)、变量(Variable)、占位符(placeholder)和张量类型转换reshape()

    常量 constant tf.constant()函数定义: def constant(value, dtype=None, shape=None, name="Const", v ...

  7. eclipse IDE注释模板设置

    1.windows->preference—>java->code style->code template,弹出下图所示的界面.

  8. javascript:delete 删除对象的属性

    delete 运算符删除对以前定义的对象属性或方法的引用. 不可以删除的如下: 1通过var定义的变量 var a=1;delete a//false 2 声明后的函数 function a(){}; ...

  9. 制作spark镜像

    构建镜像 添加jdk引用(可以使用yum进行安装): 安装SSH 碰到一个问题,执行systemctl的时候发生了异常: Failed to get D-Bus connection 解决这个问题的方 ...

  10. BZOJ3307:雨天的尾巴

    浅谈线段树合并:https://www.cnblogs.com/AKMer/p/10251001.html 题目传送门:https://lydsy.com/JudgeOnline/problem.ph ...