【题目链接】

【题意】

根据二叉树的前序和后序序列,如果中序序列唯一,输出Yes,如果不唯一输出No,并输出这个中序序列。

【题解】

众所周知,二叉树是不能够根据前序和中序建立的,为什么呢?首先需要明确先序序列的遍历顺序是:根左右,后序序列的遍历顺序是:左右根。

然后我们来说一下这个样例(为了更好的说明不唯一性,没有用题目给出的样例):

前序:1 4 6 3 2 5 7

后序:3 6 4 5 7 2 1

首先1肯定是整棵二叉树的根节点,然后根据前序序列我们可以知道4是1的子树(此时还不确定是左子树还是右子树),然后可以得到在后序序列中4的位置,由于后序序列的遍历顺序是左右根,那么很容易可以确定3和6肯定是4的子节点,这个时候我们可以知道已4为根节点的子节点有2个(代码中也就是num),然后到前序序列去算,发现prer-prel - 1(也就是去掉1 4 )发现大于num,由于前序遍历序列是根左右,所以prer - prel -1 - num > 0不就说明去掉左子树之外还存在节点嘛,那我们刚才的以4为根节点的树就是1的左子树,2 5 7即是右子树。

然后我们继续递归(也就是说把刚才的序列分成左区间(前序 4 6 3 后序 3 6 4)和右区间(前序2 5 7 后序5 7 2)),然后我们来看左子树吧,这时候可以知道6是4的子树,到后序序列里找到6,那么可以确定3是6的子节点,这时我们可以知道以6位根节点的子节点有1个,然后发现前序序列中也只有1个3,这时候我们就无法确定6是4的左子树还是右子树了。

【代码】

#include<bits/stdc++.h>
using namespace std;
const int maxn = 35;
int pre[maxn], post[maxn];
int f = 1;
vector<int>v;
void getorder(int prel, int prer, int postl, int postr)
{
if (prel == prer)
{
v.push_back(pre[prel]);
return;
}
int a = pre[prel + 1];
int postidx = postl;
while(post[postidx] != a && postidx <= postr)postidx++;
int num = postidx - postl;//以a为根节点的子树的节点数
if (prer - prel - 1 == num)f = 0; /*在前序序列中,如果以pre[prel]为根节点的子树的节点数-a这个节点等于num,4
那么久无法判断a是在pre[prel]的左子树还是右子树,这里我们默认为左子树*/
getorder(prel + 1, prel + num + 1, postl, postidx); v.push_back(pre[prel]); /*在前序序列中,如果以pre[prel]为根节点的子树的节点数-a这个节点大于num,
因为前序的遍历序列是根左右,那么就说明以pre[prel]为根的树还有右子树*/
if (prer - prel - 1> num)
getorder(prel + num + 2, prer, postidx + 1, postr - 1);
}
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", &pre[i]);
for (int i = 0; i < n; i++)
scanf("%d", &post[i]);
getorder(0, n - 1, 0, n - 1);
if(f)printf("Yes\n");
else printf("No\n");
printf("%d", v[0]);
for (int i = 1; i < n; i++)
printf(" %d", v[i]);
printf("\n");
}

【PAT甲级】1119 Pre- and Post-order Traversals(前序后序转中序)的更多相关文章

  1. PAT甲级1119. Pre- and Post-order Traversals

    PAT甲级1119. Pre- and Post-order Traversals 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二进制树可以通过给定的一对后序和顺序遍历序列来确定,也可以通 ...

  2. PAT Advance 1119 Pre- and Post-order Traversals (30) [树的遍历,前序后序转中序]

    题目 Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree c ...

  3. PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)

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

  4. PAT甲题题解-1119. Pre- and Post-order Traversals (30)-(根据前序、后序求中序)

    (先说一句,题目还不错,很值得动手思考并且去实现.) 题意:根据前序遍历和后序遍历建树,输出中序遍历序列,序列可能不唯一,输出其中一个即可. 已知前序遍历和后序遍历序列,是无法确定一棵二叉树的,原因在 ...

  5. DS实验题 Order 已知父节点和中序遍历求前、后序

    题目: 思路: 这题是比较典型的树的遍历问题,思路就是将中序遍历作为位置的判断依据,假设有个节点A和它的父亲Afa,那么如果A和Afa的顺序在中序遍历中是先A后Afa,则A是Afa的左儿子,否则是右儿 ...

  6. PAT A 1119. Pre- and Post-order Traversals (30)【二叉树遍历】

    No.1119 题目:由前序后序二叉树序列,推中序,判断是否唯一后输出一组中序序列 思路:前序从前向后找,后序从后向前找,观察正反样例可知,前后序树不唯一在于单一子树是否为左右子树. 判断特征:通过查 ...

  7. PAT甲级——A1020 Tree Traversals

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  8. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  9. PAT甲级考前整理(2019年3月备考)之一

       转载请注明出处:https://www.cnblogs.com/jlyg/p/7525244.html 终于在考前,刷完PAT甲级131道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种 ...

随机推荐

  1. USB2.0协议学习笔记---描述符

     USB设备描述符 字段名 长  度(字节)  地址偏移 含           义 bLenth   1  0  描述符长度 bDescriptorType   1  1 描述符类型 (这里为 1) ...

  2. spring boot集成mybatis只剩两个sql 并提示 Cannot obtain primary key information from the database, generated objects may be incomplete

    前言 spring boot集成mybatis时只生成两个sql, 搞了一个早上,终于找到原因了 找了很多办法都没有解决, 最后注意到生成sql的时候打印了一句话: Cannot obtain pri ...

  3. NMAP 使用教程!,nmap [Scan Type(s)] [Options] {target specification} , nmap -sn 192.168.2.0/24 , raspberry pi 3

    NMAP 使用教程 https://nmap.org/man/zh/man-briefoptions.html 当Nmap不带选项运行时,该选项概要会被输出,最新的版本在这里 http://www.i ...

  4. Expose Loader & shit jquery

    Expose Loader webpack https://github.com/xgqfrms/FEIQA/issues/31#issuecomment-418255126 require(&quo ...

  5. Python3 & Decorators with arguments & @Decorators with arguments bug

    Python3 & Decorators with arguments & @Decorators with arguments bug @Decorators with argume ...

  6. taro table component

    taro table component https://juejin.im/post/5d901696f265da5b926bbcaa https://taro-ext.jd.com/search? ...

  7. vue SSR & asyncData & nuxt.js

    vue SSR & asyncData & nuxt.js https://zh.nuxtjs.org/api/ https://www.cnblogs.com/xgqfrms/p/1 ...

  8. css icons fontawesome-free

    官网 examples v4.7.0 cdnjs icons basic-use 安装 λ npm install --save @fortawesome/fontawesome-free fa前缀在 ...

  9. rxjs 常用的subject

    api列表 Subject Subject是可观察的一种特殊类型,它允许将值多播到许多观察者 import {Subject} from 'rxjs'; const l = console.log; ...

  10. 超强嘉宾阵容——NGK Global启动大会圆满举办

    近日,由星盟全球投资公司.灵石团队联合主办的NGK Global全球生态启动大会圆满开幕.大会汇集区块链领域.金融领域.密码学领域.智能算法领域等众多大咖,和NGK Global全球价值共识者共聚一堂 ...