Source:

PAT A1020 Tree Traversals (25 分)

Description:

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

Input Specification:

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

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. 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
  2. 2 3 1 5 7 6 4
  3. 1 2 3 4 5 6 7

Sample Output:

  1. 4 1 6 3 5 7 2

Keys:

Code:

  1. /*
  2. time: 2019-06-30 14:40:45
  3. problem: PAT_A1020#Tree Traversals
  4. AC: 08:33
  5.  
  6. 题目大意:
  7. 给出后序和中序遍历,打印层序遍历
  8. */
  9. #include<cstdio>
  10. #include<queue>
  11. using namespace std;
  12. const int M=;
  13. int post[M],in[M],n;
  14. struct node
  15. {
  16. int data;
  17. node *lchild,*rchild;
  18. };
  19.  
  20. node *Create(int postL, int postR, int inL, int inR)
  21. {
  22. if(postL > postR)
  23. return NULL;
  24. node *root = new node;
  25. root->data = post[postR];
  26. int k;
  27. for(k=inL; k<=inR; k++)
  28. if(in[k] == post[postR])
  29. break;
  30. int numLeft = k-inL;
  31. root->lchild = Create(postL,postL+numLeft-,inL,k-);
  32. root->rchild = Create(postL+numLeft,postR-,k+,inR);
  33. return root;
  34. }
  35.  
  36. void LayerOrder(node *root)
  37. {
  38. queue<node*> q;
  39. q.push(root);
  40. int pt=;
  41. while(!q.empty())
  42. {
  43. root = q.front();
  44. q.pop();
  45. printf("%d%c", root->data, ++pt==n?'\n':' ');
  46. if(root->lchild)
  47. q.push(root->lchild);
  48. if(root->rchild)
  49. q.push(root->rchild);
  50. }
  51. }
  52.  
  53. int main()
  54. {
  55. #ifdef ONLINE_JUDGE
  56. #else
  57. freopen("Test.txt", "r", stdin);
  58. #endif // ONLINE_JUDGE
  59.  
  60. scanf("%d", &n);
  61. for(int i=; i<n; i++)
  62. scanf("%d", &post[i]);
  63. for(int i=; i<n; i++)
  64. scanf("%d", &in[i]);
  65. node *root = Create(,n-,,n-);
  66. LayerOrder(root);
  67.  
  68. return ;
  69. }

PAT_A1020#Tree Traversals的更多相关文章

  1. Tree Traversals

    Tree Traversals 原题链接 常见的二叉树遍历的题目,根据后序遍历和中序遍历求层次遍历. 通过后序遍历和中序遍历建立起一棵二叉树,然后层序遍历一下,主要难点在于树的建立,通过中序遍历和后序 ...

  2. HDU 1710 二叉树的遍历 Binary Tree Traversals

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  3. hdu1710(Binary Tree Traversals)(二叉树遍历)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  4. HDU1710Binary Tree Traversals

    HDU1710Binary Tree Traversals 题目大意:给一个树的前序遍历和中序遍历,要求输出后序遍历. (半年前做这道题做了两天没看懂,今天学了二叉树,回来AC了^ ^) 首先介绍一下 ...

  5. HDU-1701 Binary Tree Traversals

    http://acm.hdu.edu.cn/showproblem.php?pid=1710 已知先序和中序遍历,求后序遍历二叉树. 思路:先递归建树的过程,后后序遍历. Binary Tree Tr ...

  6. 03-树2. Tree Traversals Again (25)

    03-树2. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...

  7. HDU 1710-Binary Tree Traversals(二进制重建)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  8. PAT1086:Tree Traversals Again

    1086. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  9. Binary Tree Traversals(HDU1710)二叉树的简单应用

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

随机推荐

  1. python的代码块缓存机制,小数据池机制。

    同一代码块的缓存机制 在python中一个模块,一个函数,一个类,一个文件等都是一个代码块. 机制内容:Python在执行同一个代码块的初始化对象的命令时,会检查是否其值是否已经存在,如果存在,会将其 ...

  2. jmeter登录之-动态参数

    jmeter登录之-动态参数 1.抓包查看提交的登录参数 发现参数authenticity_token是动态的,每次都不一样,所以回放的时候就会失败 2.提取动态变化的参数-后置处理器(相当于LR的关 ...

  3. VC内联汇编,引用程序中的变量

    int a=5; //变量a _asm { mov eax,a;       //将变量a的值放入寄存器eax add eax,eax;   //相当于a=a+a mov a,eax;      // ...

  4. elasticsearch5.5.2安装

     elasticsearch5.x安装中一些问题的解决办法 最近在学习elk,由于编译安装使用5.2.1版本的elasticsearch,所以遇到了很多问题,下面是一些问题及解决办法. 1.修改访问e ...

  5. Ubuntu管理员密码设置

    最近学习嵌入式编程,首先准备搭建一个嵌入式开发环境. 由于想省钱,就准备搭建一个虚拟的arm系统用于测试学习. 虚拟系统搭建与linux系统上,暂定使用Ubuntu+qemu进行环境搭建. 在进行Ub ...

  6. docker集群故障迁移

    docker swarm 故障时候镜像迁移(无法添加新节点的时候)生产docker集群出现了故障,无法正常添加删除节点.在这样的情况下只能想办法把故障集群的镜像迁移到新的docker集群当中.将发生故 ...

  7. 实现Comparator接口和Comparable接口,以及Map按value排序 ,map遍历

    继承Comparator接口,重写compare()方法 import java.util.ArrayList; import java.util.Arrays; import java.util.C ...

  8. c#委托(Delegates)--基本概念及使用 转发

    在我这菜鸟理解上,委托就是可以用方法名调用另一方法的便捷方法,可以简化switch等语句的重复.最近做项目的时候恰好需要用到委托,便来复习及学习委托的使用.嗯...本人以前并没有用过,只是稍微知道而已 ...

  9. 2.4 webpack + gulp 构建完整前端工作流

    在前面的两个小节中已经完整的讲了 webpack 和 gulp 相关的内容,本小节中将会结合二者构建一个完整的前端工作流,内容目录为: 前端工程结构和目标 前端工程目录结构 gulp clean gu ...

  10. 1085 Perfect Sequence (25 分)

    Given a sequence of positive integers and another positive integer p. The sequence is said to be a p ...