PAT 1043【BST与二叉树】
考察:
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与二叉树】的更多相关文章
- 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 ...
- PAT归纳总结——关于二叉树的一些总结
今天是6月26日到下个月的这个时候已经考过试了,为了让自己考一个更高的分数,所以我打算把PAT的相关题型做一个总结.目前想到的方法就是将相关的题型整理到一起然后,针对这种题型整理出一些方法. 二叉树的 ...
- 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 ...
- 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 L2-011 玩转二叉树
https://pintia.cn/problem-sets/994805046380707840/problems/994805065406070784 给定一棵二叉树的中序遍历和前序遍历,请你先将 ...
- PAT L2-011 玩转二叉树(二叉树层序遍历)
给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列.所谓镜面反转,是指将所有非叶结点的左右孩子对换.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出 ...
- PAT 1043 输出PATest(20)(代码+思路)
1043 输出PATest(20)(20 分) 给定一个长度不超过10000的.仅由英文字母构成的字符串.请将字符重新调整顺序,按"PATestPATest...."这样的顺序输出 ...
- PAT 1020 Tree Traversals[二叉树遍历]
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT——1043. 输出PATest
给定一个长度不超过10000的.仅由英文字母构成的字符串.请将字符重新调整顺序,按“PATestPATest....”这样的顺序输出,并忽略其它字符.当然,六种字符的个数不一定是一样多的,若某种字符已 ...
随机推荐
- 英语发音规则---Y字母
英语发音规则---Y字母 一.总结 一句话总结: 1.Y字母在单词最前面读发[j]? yes /jes/ [jɛs] n. 是 yard /jɑːd/ [jɑd] n. 院子 yellow /'jel ...
- jQuery - 获取/设置内容和属性
获得内容 - text().html() 以及 val() 三个简单实用的用于 DOM 操作的 jQuery 方法: text() - 设置或返回所选元素的文本内容 html() - 设置或返回所选元 ...
- 十三 Django框架,CSRF跨站请求伪造
全局CSRF 如果要启用防止CSRF跨站请求伪造,就需要在中间件开启CSRF #中间件 MIDDLEWARE = [ 'django.middleware.security.SecurityMidd ...
- the referenced script on this behaviour is missing!
1.看看你脚本上挂的某个组件是不是发生了变动,比如被删除了什么的 2.最有可能的是你创建完脚本后,中途改过脚本的名字,致使脚本名字和内部的名字不统一.
- IO - 文件的读取与写入
最近有较多提取文档内容,然后拼接成sql之类的,但是纯粹的复制粘贴又太傻,就写了一个脚本,自动读取文件内容(word文档可能需要复制成txt文本),然后拼接sql,最后写入到指定文件中,试了下还是蛮好 ...
- 每天一个linux命令(7):rmdir命令
版权声明更新:2017-05-11博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下面的rmdir命令. ...
- 【LeetCode】019. Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- bzoj 3083 遥远的国度 —— 树链剖分
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3083 换根后路径还是不变,子树分类讨论一下,树剖后线段树维护即可. 代码如下: #inclu ...
- NB-IoT知识
通常,我们把物联网设备分为三类: ①无需移动性,大数据量(上行),需较宽频段,比如城市监控摄像头. ②移动性强,需执行频繁切换,小数据量,比如车队追踪管理. ③无需移动性,小数据量,对时延不敏感,比如 ...
- selenium如何获取已定位元素的属性值?
HTML源代码: <div class="res-status" data-fortune="5" data-selfsos="" d ...