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....”这样的顺序输出,并忽略其它字符.当然,六种字符的个数不一定是一样多的,若某种字符已 ...
随机推荐
- css 中 div垂直居中的方法
在说到这个问题的时候,也许有人会问CSS中不是有vertical-align属性来设置垂直居中的吗?即使是某些浏览器不支持我只需做少许的CSS Hack技术就可以啊!所以在这里我还要啰嗦两句,CSS中 ...
- POJ 3126 Prime Path(BFS算法)
思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...
- django admin后台显示H5颜色选项卡
一般使用django后台,特别是有颜色的字段的时候避免手动输入一大推的颜色,可以使用h5颜色选项卡,这样用户就直接可以通过选项卡来选择自己所需的颜色.这个时候可以通过后台admin form来进行定制 ...
- cluster KMeans need preprocessing scale????
Out: n_digits: 10, n_samples 1797, n_features 64 ___________________________________________________ ...
- linux命令学习笔记(16):which命令
我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索: which 查看可执行文件的位置. whereis 查看文件的位置. locate 配合数据库查看文件位置. f ...
- FFMPEG相关开源项目
1.FFmpeg build for android random architectures with example jnihttps://github.com/appunite/AndroidF ...
- Skype SILK vs. iLBC vs. Speex
对比一下这三种VOIP语音算法的特点: 1 参数与特征 2 SILK性能 关于iLBC和Speex的性能可以参考以前写的文章. 3 关于VOIP一些观点(仅代表个人观点) 1) Skype 辛苦三年 ...
- JS数组的sort排序
数组sort方法排序var aa=[6,2,1,5]//默认是从小到大排序aa.sort()[1, 2, 5, 6] //下面也是从小到大排序aa.sort(function(a,b){return ...
- C语言计算日期间隔天数的经典算法解析
#include <stdio.h> #include <stdlib.h> int day_diff(int year_start, int month_start, int ...
- C++中的对象的赋值和复制
对象的赋值 如果对一个类定义了两个或多个对象,则这些同类的对象之间可以互相赋值,或者说,一个对象的值可以赋给另一个同类的对象.这里所指的对象的值是指对象中所有数据成员的值. 对象之间的赋值也是通过赋值 ...