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....”这样的顺序输出,并忽略其它字符.当然,六种字符的个数不一定是一样多的,若某种字符已 ...
随机推荐
- Linux课程---11、Linux中软件安装和调试
Linux课程---11.Linux中软件安装和调试 一.总结 一句话总结: 启动过程:1.安装软件,2.修改配置文件,3.启动服务 查看过程:4.查看进程,5.查看端口 关闭过程:6.关闭软件,7. ...
- java:Eclipse插件springsource-tool-suite的下载和安装
1.打开下载页面http://spring.io/tools/sts/all 找到这个,后补全部版本链接http://spring.io/tools/sts/legacy 插件压缩包下载安装: 链接下 ...
- WingIDE用法笔记
注释代码块 方法一: ''' 被注释的代码块 ''' 方法二: 选中要注释的代码块后 Ctrl + /,则选中的每一行都被# , 用这种方法注释的代码,用Shift + Ctrl + / ...
- Android 内存监测工具 DDMS --> Heap
用 Heap监测应用进程使用内存情况的步骤如下: 1. 启动eclipse后,切换到DDMS透视图,并确认Devices视图.Heap视图都是打开的: 2. 将手机通过USB链接至电脑,链接时需要确认 ...
- 【leetcode刷题笔记】Populating Next Right Pointers in Each Node II
What if the given tree could be any binary tree? Would your previous solution still work? Note: You ...
- uoj279温暖会指引我们前行
暖气来啦~ 动态树维护最大生成树裸题 #include<iostream> #include<cstdio> #include<cstdlib> #include& ...
- ACM学习历程—HDU1041 Computer Transformation(递推 && 大数)
Description A sequence consisting of one digit, the number 1 is initially written into a computer. A ...
- Redis的安装和配置文件
实验环境:Centos6.8 Redis版本:3.0.6 下载Redis,并放到/usr/local/soft下: yum -y install gcc automake autoconf libto ...
- 面向对象(Java中普通代码块,构造代码块,静态代码块区别及代码示例)
//执行顺序:(优先级从高到低.)静态代码块>mian方法>构造代码块>构造方法. 其中静态代码块只执行一次.构造代码块在每次创建对象是都会执行. 1 普通代码块 //普通代码块:在 ...
- centos MAC 地址与报错eth0 unknown interface no such device
eth0 unknown interface no such device 出现这个原因是由于虚拟机直接COPY过来,MAC地址发生了变化,但eth0 里仍然记录着旧的MAC地址. 解决方法: vim ...