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.

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. using namespace std;
  5. const int inf = ;
  6. const int maxn = ;
  7. int n;
  8. int pre[maxn],post[maxn];
  9. int Tree[maxn][];
  10. int flag=true;
  11. int buildTree(int l1,int r1,int l2,int r2){
  12. if(r1<l1) return -;
  13. int root=pre[l1];
  14. if(l1==r1){
  15. Tree[root][]=-;
  16. Tree[root][]=-;
  17. return root;
  18. }
  19. if(l1+==r1){
  20. Tree[root][]=pre[l1+];
  21. Tree[root][]=-;
  22. flag=false;
  23. return root;
  24. }
  25. int i;
  26. for(i=l2;post[i]!=pre[l1+];i++);
  27. Tree[root][]=buildTree(l1+, l1++i-l2, l2, i);
  28. Tree[root][]=buildTree(l1++i-l2, r1, i+, r2-);
  29. return root;
  30. }
  31. int cnt=;
  32. void inOrderTra(int v){
  33. if(v==-){
  34. return;
  35. }
  36. inOrderTra(Tree[v][]);
  37. if(cnt) printf(" ");
  38. printf("%d",v); cnt++;
  39. inOrderTra(Tree[v][]);
  40. }
  41. int main(){
  42. fill(Tree[], Tree[]+maxn*, -);
  43. scanf("%d",&n);
  44. for(int i=;i<n;i++){
  45. scanf("%d",&pre[i]);
  46. }
  47. for(int i=;i<n;i++){
  48. scanf("%d",&post[i]);
  49. }
  50. int Root=buildTree(,n-,,n-);
  51. if(flag) printf("Yes\n");
  52. else printf("No\n");
  53. inOrderTra(Root);
  54. printf("\n");
  55. }

1119 Pre- and Post-order Traversals的更多相关文章

  1. Construct a tree from Inorder and Level order traversals

    Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree. Following is a ...

  2. [LeetCode] Rank Scores 分数排行

    Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...

  3. HDU 4358 Boring counting(莫队+DFS序+离散化)

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others) ...

  4. ASP.NET MVC : Action过滤器(Filtering)

    http://www.cnblogs.com/QLeelulu/archive/2008/03/21/1117092.html ASP.NET MVC : Action过滤器(Filtering) 相 ...

  5. HDU 1160 FatMouse's Speed

    半个下午,总算A过去了 毕竟水题 好歹是自己独立思考,debug,然后2A过的 我为人人的dp算法 题意: 为了支持你的观点,你需要从给的数据中找出尽量多的数据,说明老鼠越重速度越慢这一论点 本着“指 ...

  6. UVA 1175 Ladies' Choice 稳定婚姻问题

    题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ...

  7. Spring Cloud Zuul 限流详解(附源码)(转)

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

  8. [LeetCode] 系统刷题4_Binary Tree & Divide and Conquer

    参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历. 此处说明Divi ...

  9. LeetCode: Recover Binary Search Tree 解题报告

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

  10. [LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal

    Pre: node 先,                      Inorder:   node in,           Postorder:   node 最后 PreOrder Inorde ...

随机推荐

  1. 【Servlet】(1)Servlet简介、Servlet底层原理、Servlet实现方式、Servlet生命周期

    一.Servlet简介 1.Servlet定义: Servlet(Server Applet)是Java Servlet的简称,是为小服务程序或服务连接器,用Java编写的服务器端程序,主要功能在于交 ...

  2. WOW

    WOW http://bbs.ngacn.cc/read.php?tid=4992959  http://ngasave.us/popcn/?t=gems  地精科技:国服最流行 http://bbs ...

  3. 10. 批量插入List<String>

    List<String> iscBusOrgIdList = getIscOrgIdList();List<Map<String, Object>> iscBusO ...

  4. UI5-培训篇-Fiori培训

    1.学习网站: SAPUI5学习地址: https://blog.csdn.net/stone0823/article/category/6650292/1? SAPUI5文档: https://sa ...

  5. MySQL 8.0 新增SQL语法对窗口函数和CTE的支持

    尝试了一下MySQL 8.0的部分新特性. 如果用过MSSQL或者是Oracle中的窗口函数(Oracle中叫分析函数), 然后再使用MySQL 8.0之前的时候,就知道需要在使用窗口函数处理逻辑的痛 ...

  6. install mysql from source and troubleshooting example

    I tried to install MySQL 5.7 from source file and upgrading previous MySQL version to the lastest 5. ...

  7. Modelsim command line 传参数到 .do 文件

    gui跑mdelsim总觉得很麻烦,使用命令来启动方便了很多,类似linux一样,其实目前windows也可以做到,只是业界不怎么用windows罢了. 基于modelsim搭了一个UVM环境,  用 ...

  8. Shiro Remember me设置

    1. 在Spring的相关配置文件中加入如下Remember me管理器配置: <!-- rememberMe管理器 --> <bean id="rememberMeMan ...

  9. centos系统有多个内核,修改默认启动内核

    打开系统文件 vim  /boot/grub/grub.conf default=0,意思是GRUB在默认情况下,也就是用户没有选择的情况下,去启动显示在用户界面的第一个系统:GRUB启动系统时是从0 ...

  10. flash/flex builder在IE中stage.stageWidth始终为0的解决办法

    这应该是IE的bug,解决办法: 原作者:菩提树下的杨过出处:http://yjmyzz.cnblogs.com stage.align=StageAlign.TOP_LEFT; stage.scal ...