考察:

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. ajax如何处理返回的数据格式是xml的情况

    <!DOCTYPE html> <html> <head> <title>用户注册</title> <meta charset=&qu ...

  2. 聊聊js跨域

    推荐先读一下这篇文章: https://segmentfault.com/a/1190000012469713http://www.dailichun.com/2017/03/22/ajaxCross ...

  3. Confluence 6 找到未使用的空间

    有时候,你希望找到你系统中没有使用的内容.有时候你也希望能够对这些内容进行更多的关注,但是如何找到一些有关长期不更新的页面,或者长期不使用的空间? View Space Activity 页面中的内容 ...

  4. Java_异常_06_ Unsupported major.minor version 52.0

    二.参考资料 1.如何解决Unsupported major.minor version 52.0问题? 2.Unsupported major.minor version 52.0 3. Unsup ...

  5. python TypeError: 'NoneType' object is not iterable

    list(set(map(lambda tp_id : tp_id if not ('#' in tp_id) and len(tp_id.strip().replace('\n', '')) > ...

  6. 机器学习 Support Vector Machines 1

    引言 这一讲及接下来的几讲,我们要介绍supervised learning 算法中最好的算法之一:Support Vector Machines (SVM,支持向量机).为了介绍支持向量机,我们先讨 ...

  7. 【LeetCode】084. Largest Rectangle in Histogram

    题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...

  8. DEBUG命令详细说明

    启动DEBUG 1.打开Windows命令窗口 在Windows 95/98的环境中,打开命令窗口的步骤为:点击“开始”→“运行”,输入“command”命令: 在WindowsXP及WIN7的环境中 ...

  9. jumpserver遇到的坑

    安装:https://github.com/jumpserver/jumpserver,看readme照着做就行,下面是遇到的坑.   0.4.4版坑: 1.要升级pip,否则有的包装不上   2.p ...

  10. 办公软件-Excel:Microsoft Office Excel 2003百科

    ylbtech-办公软件-Excel:Microsoft Office Excel 2003百科 Microsoft® Office Excel 2003 是一种电子表格程序,可提供对于 XML 的支 ...