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 (≤30), 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

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <algorithm>
  5. #include <iostream>
  6. #include <string.h>
  7. #include <queue>
  8. #include <string>
  9. #include <set>
  10. #include <map>
  11. using namespace std;
  12. const int maxn = ;
  13. int n;
  14. int post[maxn], in[maxn];
  15. struct node {
  16. int data;
  17. node* left;
  18. node* right;
  19. };
  20. void layerorder(node* root) {
  21. queue<node*> q;
  22. q.push(root);
  23. int count = ;
  24. while (!q.empty()) {
  25. node* now = q.front();
  26. q.pop();
  27. printf("%d", now->data);
  28. count++;
  29. if (now->left != NULL) q.push(now->left);
  30. if (now->right != NULL) q.push(now->right);
  31. if (count != n) printf(" ");
  32. }
  33. }
  34. node* create(int postl, int postr, int inl, int inr) {
  35. if (postl > postr) {
  36. return NULL;
  37. }
  38. node* root = new node;
  39. root->data = post[postr];
  40. int k;
  41. for (k = inl; k <= inr; k++) {
  42. if (in[k] == post[postr]) {
  43. break;
  44. }
  45. }
  46. int leftnum = k - inl;
  47. root->left = create(postl, postl + leftnum - , inl, k-);
  48. root->right = create(postl + leftnum, postr - , k + , inr);
  49. return root;
  50. }
  51. int main() {
  52. cin >> n;
  53. for (int i = ; i < n; i++) {
  54. cin>>post[i];
  55. }
  56. for (int i = ; i < n; i++) {
  57. cin >> in[i];
  58. }
  59. node* root = create(, n - , , n - );
  60. layerorder(root);
  61. system("pause");
  62. }

注意点:考察基本的二叉树遍历,难点在递归上,想清楚了递归边界和递归式就简单了。二叉树的遍历及建树还需要巩固。

PAT A1020 Tree Traversals (25 分)——建树,层序遍历的更多相关文章

  1. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

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

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

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

  3. PAT Advanced 1020 Tree Traversals (25 分)

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

  4. PAT A1020 Tree Traversals(25)

    题目描述 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder ...

  5. 1020 Tree Traversals (25 分)

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

  6. 1020 Tree Traversals (25分)思路分析 + 满分代码

    题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...

  7. 【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)

    题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...

  8. A1020. Tree Traversals(25)

    这是一题二叉树遍历的典型题,告诉我们中序遍历和另外一种遍历序列,然后求任何一种遍历序列. 这题的核心: 建树 BFS #include<bits/stdc++.h> using names ...

  9. [PAT] A1020 Tree Traversals

    [题目] distinct 不同的 postorder 后序的 inorder 中序的 sequence 顺序:次序:系列 traversal 遍历 题目大意:给出二叉树的后序遍历和中序遍历,求层次遍 ...

随机推荐

  1. 【Quartz】1、Quartz使用说明

    简介 Quartz 是个开源的作业调度框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制.Quartz 允许开发人员根据时间间隔(或天)来调度作业.它实现了作业和触发器的多对多关系,还 ...

  2. csharp: Configuring ASP.NET with Spring.NET and FluentNHibernate

    Domain: FluentNhibernateLocalSessionFactoryObject.cs using System; using System.Collections.Generic; ...

  3. 【读书笔记】iOS-单元测试工具

    一,单元测试工具. 1,OCUnit 苹果自带的. 2,GHUnit GHUnit是Objective-C语言里的另外一种单元测试工具. GHUnit- https://github.com/gh-u ...

  4. 使用jquery获取iframe内的元素属性

    当需要获取iframe里的内容时需要有几个前提,否则你是获取不到的: 1:当前页面与iframe的src的页面需要在同一个域名下: 2:必须要等iframe里边的页面加载完成才能获取,否则你要获取的标 ...

  5. Mongodb的入门(8)mongodb事物分析

    老生常谈:<在前面博客中也介绍过> mongodb官网:https://docs.mongodb.com/manual/introduction/ mongodb:官网上是这样定义的Mon ...

  6. 用LinQ扩展方法,泛型扩展方法,实现自定义验证字符是否空、对象是否为null,及泛型约束使用,Action的使用

    一.Linq扩展方法 1.扩展方法必须是静态方法.扩展方法所在的类必须是静态类 2.扩展方法里面的参数必须制定this关键字,紧跟需要扩展的类型,如下: 二.泛型约束 1.使用泛型的原因,是在不知道需 ...

  7. Android 发送自定义广播(标准和本地)

    1.首先自定义一个广播接收器:MyBroadcastReceiver package example.com.mybroadcastreceiver; import android.content.B ...

  8. php二维数组去重

    php二维数组去重 前言:php一维数组去重很简单,直接array_unique($arr)即可,但是二维数组去重就得自己去写了 二维数组去重方法: /* * 二维数组去重 * 注意:二维数组中的元素 ...

  9. Ubuntu关闭(重启)网络服务命令

    Ubuntu关闭网络服务命令: service network-manager stop 重启网络服务命令: service network-manager restart

  10. 彻底卸载删除Win10易升,禁止再生

    易升是微软推出的win10升级工具.用户可通过易升一键升级win10. 因为我的电脑已经是win10的系统,所以我也不需要升级.也不想升级,因为我从网上了解到升级后的系统反而没有升级前的好用. 微软的 ...