原题传送门

  • 树的遍历 之 已知后序和中序输出层序
#include <iostream>
#include <vector>
using namespace std;vector<int> post, in, level(100000, -1);void pre(int root, int start, int end, int index) {if(start > end)return;int i = start;while(i < end && in[i] != post[root])++i;level[index] = post[root];pre(root-1-(end-i), start, i-1, 2*index+1);pre(root-1, i+1, end, 2*index+2);
}int main(int argc, const char * argv[]) {int n;cin >> n;post.resize(n);in.resize(n);for(int i = 0; i < n; ++i)cin >> post[i];for(int i = 0; i < n; ++i)cin >> in[i];pre(n-1, 0, n-1, 0);cout << level[0];for(int i = 1; i < level.size(); ++i) {if(level[i] != -1)cout << " " << level[i];}return 0;
}

附原题:

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:
    7
    2 3 1 5 7 6 4
    1 2 3 4 5 6 7
  • Sample Output:
    4 1 6 3 5 7 2

PAT 甲级 1020. Tree Traversals相关推荐

  1. PAT甲级1020 Tree Traversals:[C++题解]树的遍历、由中序序列和后序序列递归建树

    文章目录 题目分析 题目链接 题目分析 题意重述:给定一棵二叉树的后序遍历序列和中序遍历序列,让求层次遍历的序列. 分析: 后序遍历:先 左子树.右子树 ,最后再遍历根结点. 中序遍历:先左子树,再根 ...

  2. PAT甲级1086 Tree Traversals Again:[C++题解]二叉树中序序列、栈、求后序遍历

    文章目录 题目分析 题目链接 题目分析 分析: 给定栈模拟的二叉树的中序序列. 我们可以发现一些性质: 1 第一个值是根结点. 2 对于所有的push操作,如果上一个是push,该结点就是上一个结点的 ...

  3. 1020. Tree Traversals (25)

    题目链接:http://www.patest.cn/contests/pat-a-practise/1020 题目: 1020. Tree Traversals (25) 时间限制 400 ms 内存 ...

  4. PAT甲级1020(附带前中序遍历の绝对干货)

    引子 我之前对遍历推导这块没怎么动脑子,凭感觉.所以这题是先参考了百度经验,别笑,这个链接,我觉得讲得非常清楚. https://jingyan.baidu.com/article/cdddd41cb ...

  5. PAT甲级1020变体:已知二叉树层序+中序序列,求后序遍历序列

    PAT甲级1020变体:已知二叉树层序+中序序列,求后序遍历序列 题目 输入格式 输出格式 输入样例 输出样例 代码 题目 已知二叉树层序+中序序列,求后序遍历序列. 输入格式 第一行给出该二叉树的节 ...

  6. 【详细注解】1020 Tree Traversals (25 分)

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Suppose that all the keys in a binary tree are distinct positive ...

  7. 1020. Tree Traversals (25) PAT甲级真题

    之前我看了这道题,实在是看不懂网上的解题答案,他们的具体思路基本上就是通过后续遍历和中序遍历,直接推出层次遍历. 我苦思冥想了半天,是在没看懂这种思路,于是想了一个笨点的但是也比较好理解的思路,通过后 ...

  8. 【PAT - 甲级1020】Tree Traversals (25分)(树的遍历,给定中序后序,求层次遍历)

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

  9. PAT 1020. Tree Traversals

    题目:http://pat.zju.edu.cn/contests/pat-a-practise/1020 题解: 题意就是给一个二叉树的后序和中序,然后确定这棵二叉树,然后层序输出这棵树. 主要思路 ...

  10. 1020 Tree Traversals

    1. 有这样一个经典结论:中序序列可以和先序序列.后序序列.层序序列中的任意一个来构建唯一的二叉树,而后三者两两搭配或者三个一起上都不行.因为从本质上来说,后三者都只提供根结点,只有通过中序才能区分左 ...

最新文章

  1. 如何使用JavaScript Math.floor生成范围内的随机整数-已解决
  2. android 8.0 3D锁屏,Android 8.0 锁屏滑动无法解锁
  3. 1.1.3 计算机的分类与发展方向(电子计算机、计算机按指令和数据流分类、计算机发展趋势正向着“两级”分化)
  4. 反思走火入魔 急功近利:一步一步的做 一步一步的走 顺其自然 水到渠成
  5. 【Java入门】桌球小游戏
  6. 带你学会区分Scheduled Thread Pool Executor 与Timer
  7. [Objective C] 获取类和对象的运行时状态
  8. 数据科学包7- pandas高级内容之分组运算
  9. 详解百度地图API之地图操作
  10. 新观察:企业将全面上云 披露“云计算支出”会是新常态
  11. 简单的扫雷游戏代码(c++)
  12. 地理加权回归的学习(地理加权回归用来量化空间异质性)
  13. python中day_python day02
  14. opencv与darknet在python2.7下报错问题
  15. TNS-12547和TNS-12555错误解决
  16. 清除恶意主页www.7939.com
  17. JVM语言Xtend优缺点速览
  18. 搭建2048网页小游戏
  19. 德国奔驰、博世和保时捷的员工年薪有多少?
  20. [软件工程] 面向对象分析

热门文章

  1. 全栈云服务是个什么东东?!
  2. 提示https不安全的原因及解决办法
  3. Windows系统压缩卷时可压缩空间远小于实际剩余空间解决方法
  4. EAP设备自动化系统基本功能与概念
  5. Oracle中user_tab_cols、user_tab_columns的差异
  6. 行测-图形推理-7-相异图形类
  7. 如何获取win10锁屏界面的壁纸
  8. win10自带计算机恢复,预装Win10的创建恢复介质与恢复的步骤
  9. 长方形面积计算机方式,《长方形面积的计算》教学设计
  10. Unity线性空间UI制作方面总结