1119 Pre- and Post-order Traversals

分数 30

作者 CHEN, Yue

单位 浙江大学

Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversal sequences. However, if only the postorder and preorder traversal sequences are given, the corresponding tree may no longer be unique.

Now given a pair of postorder and preorder traversal sequences, you are supposed to output the corresponding inorder traversal sequence of the tree. If the tree is not unique, simply output any one of them.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 30), the total number of nodes in the binary tree. The second line gives the preorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first printf in a line Yes if the tree is unique, or No if not. Then print in the next line the inorder traversal sequence of the corresponding binary tree. If the solution is not unique, any answer would do. It is guaranteed that at least one solution exists. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input 1:

7
1 2 3 4 6 7 5
2 6 7 4 5 3 1

Sample Output 1:

Yes
2 1 6 4 7 3 5

Sample Input 2:

4
1 2 3 4
2 4 3 1

Sample Output 2:

No
2 1 3 4

给出前序遍历和后序遍历有可能不能唯一的确定一棵二叉树;
 * 由于前序遍历访问节点顺序是:根,左,右;
 *  后序遍历访问节点顺序是:左,右,根;
 * 所以当一棵树只有左子树或者右子树时,那么这棵树就不是唯一的,因为子树
 * 既可以在左子树上,也可以在右子树上,这都符合给出的前序遍历和后序遍历;
 * 所以当一棵树(或者子树)的只有单边子树时,那么这棵二叉树就不是唯一的;
 *
 * 又由于题目要求随意给出一棵符合要求的二叉树,不妨先建立左子树,再建立
 * 右子树,即只有单边子树的时候将其连接到左子树上;
 *
 * 首先找到左子树的根节点值v(preL+1位置处的值),根据后序遍历和v来确定
 * 左子树的节点个数;递归的建立左子树以及右子树,由于此处需要用到
 * pre[preL + 1]的值,因此当preL == preR 的时候,就需要单独处理一下,
 * 防止数组越界;

/*** 给出前序遍历和后序遍历有可能不能唯一的确定一棵二叉树;* 由于前序遍历访问节点顺序是:根,左,右;*  后序遍历访问节点顺序是:左,右,根;* 所以当一棵树只有左子树或者右子树时,那么这棵树就不是唯一的,因为子树* 既可以在左子树上,也可以在右子树上,这都符合给出的前序遍历和后序遍历;* 所以当一棵树(或者子树)的只有单边子树时,那么这棵二叉树就不是唯一的;* * 又由于题目要求随意给出一棵符合要求的二叉树,不妨先建立左子树,再建立* 右子树,即只有单边子树的时候将其连接到左子树上;* * 首先找到左子树的根节点值v(preL+1位置处的值),根据后序遍历和v来确定* 左子树的节点个数;递归的建立左子树以及右子树,由于此处需要用到* pre[preL + 1]的值,因此当preL == preR 的时候,就需要单独处理一下,* 防止数组越界;
*/#include <iostream>
#include <algorithm>using namespace std;typedef struct TNode* Bin;
struct TNode
{int v;Bin l, r;
};
const int N = 35;
int pre[N], beh[N];
bool Unique = true,  spa = false;Bin Create(int preL, int preR, int behL, int behR)
{if(preL > preR) return NULL;//由于此处需要用到//pre[preL + 1]的值,因此当preL == preR 的时候,就需要单独处理一下,// 防止数组越界;if(preL == preR){Bin t = new TNode;t->v = pre[preL];t->l = NULL;t->r = NULL;return t;}int v = pre[preL+1]; //左子树的根节点int k;for(k = behL; k <= behR && beh[k] != v; ++k);int numL = k - behL + 1; //得到左子树的节点个数if(preR - preL == numL) //如果是单子树,那么二叉树不唯一Unique = 0;Bin t = new TNode;t->v = pre[preL];t->l = Create(preL+1, preL+numL, behL, k); //左子树t->r = Create(preL+numL+1, preR, k+1, behR-1); //右子树return t;
}void mid_Traver(Bin T)
{if(T){mid_Traver(T->l);if(spa) cout << ' ';else spa = true;cout << T->v;mid_Traver(T->r);}
}
int main()
{int n;cin >> n;for(int i=0; i<n; ++i)  cin >> pre[i];for(int i=0; i<n; ++i)  cin >> beh[i];Bin Tr = Create(0, n-1, 0, n-1);if(Unique)  puts("Yes");else puts("No");mid_Traver(Tr);puts("");return 0;
}

PAT A1119 Pre- and Post-order Traversals相关推荐

  1. 【PAT甲级】1146 Topological Order

    ✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343

  2. 数据库中各表关联图及其说明_如何在图中思考:图论及其应用的说明性介绍

    数据库中各表关联图及其说明 by Vardan Grigoryan (vardanator) 由Vardan Grigoryan(vardanator) 如何在图中思考:图论及其应用的说明性介绍 (H ...

  3. 算法唯手熟尔(PAT剩余清单 or leetcode)---希望可以日更

    文章目录 2020/3/5 PAT A1119_C 2020/3/6 PAT A1123_C PAT A1115_C PAT A1114_C leetcode 206. 反转链表_C leetcode ...

  4. Zuul:Pre和Post过滤器(下)

    可以看到所有的请求都会经过他,那么我们现在要对这个请求做一个权限的校验,加入没有Zuul这个服务,那你Server A/B/C,都要校验一次,这个每个服务都要做一次,所以权限校验我们可以放到Zuul统 ...

  5. PostgreSQL数据库图像搜索插件imgsmlr部署

    https://github.com/postgrespro/imgsmlr 1.deploy apt-get install postgresql-server-dev-allapt-get ins ...

  6. Spring Cloud限流详解(附源码)

    在高并发的应用中,限流往往是一个绕不开的话题.本文详细探讨在Spring Cloud中如何实现限流. 在 Zuul 上实现限流是个不错的选择,只需要编写一个过滤器就可以了,关键在于如何实现限流的算法. ...

  7. python镜像_Python二叉树的镜像转换实现方法示例

    本文实例讲述了Python二叉树的镜像转换实现方法.分享给大家供大家参考,具体如下: 问题描述 操作给定的二叉树,将其变换为源二叉树的镜像. 思路描述 1. 代码比文字更直观 2. 文字描述:新建一个 ...

  8. Spring Cloud限流详解(内含源码)

    为什么80%的码农都做不了架构师?>>>    原文:http://www.itmuch.com/spring-cloud-sum/spring-cloud-ratelimit/ 在 ...

  9. mysql查询id为偶数_MySQL中查询中位数?

    导读:计算中位数可能是小学的内容,然而在数据库查询中实现却并不是一件容易的事.我们今天就来看看都有哪些方法可以实现. 注:本文所用MySQL版本无限制,所列题目均来源于LeetCode. LeetCo ...

最新文章

  1. Delphi 2010 secondsBetween Bug
  2. python大于等于怎么表示_如何在rejectdb中应用python lambda表达式中的大于等于
  3. 一些经常在建站中用到的英文
  4. 浅谈SQL Server 对于内存的管理
  5. SpringMVC常见面试题(5个最常见面试题,回答超详细)
  6. ant design 预览图片_AntD框架的upload组件上传图片时遇到的一些坑
  7. javascript实现页面中回到顶部功能
  8. 编写MR代码中,JAVA注意事项
  9. java - What is a fat JAR? - Stack Overflow
  10. 采用HTML5之“data
  11. 计算机系徽 节徽设计,数学节节徽设计图
  12. 激光防护屏 激光防护屏
  13. 北美票房:《玻璃先生》无悬念夺魁
  14. 南澳大学计算机科学专业学费,2020年南澳大学学费(本科及研究生)及学费支付方式解析!...
  15. 最新手机语音助手的调研
  16. 电脑硬件知识入门之内存篇
  17. 二级分销系统对企业来说意味着什么?
  18. centos 设置为北京时间
  19. 成功必须靠自己去争取。
  20. Chapter9.2:线性系统的状态空间分析与综合(上)

热门文章

  1. Pythonista——一个随时随地写Python代码的神器
  2. 潘伟明:人工智能对人类的特殊价值
  3. 锐捷防火墙RG-WALL 1600-M6600E配置
  4. 分享企业融资技巧与方法及常见有效融资途径
  5. Navicat Premium怎么更改为中文/英文
  6. 逆变器的输出外特性分析
  7. Angular 2+ Material Design Admin Template
  8. 基于stm32单片机的WIFI智能联网天气预报自动校时系统(源码+原理图+全套资料)
  9. 51单片机(三十)—— 矩阵键盘计算器
  10. uniapp开发:uniapp之切换vue3,一直使用一直爽