立志用最少的代码做最高效的表达


PAT甲级最优题解——>传送门


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


题意:给定中序、后序序列,求层序序列。

模板题。 注解已在代码中标出。 如有疑问请评论区留言~


#include<bits/stdc++.h>
using namespace std;struct Node{int data;           //值 Node *left, *right; //左节点、右节点 Node(int d) : data(d), left(nullptr), right(nullptr) {}
};
int postOrder[30], inOrder[30];     //存储后序遍历、中序遍历序列Node* createTree(int L1, int R1, int L2, int R2) {if(L1 > R1) return nullptr;Node* r = new Node(postOrder[R2]);int p = L1;while(inOrder[p] != postOrder[R2]) p++;int cnt = p-L1;            //左子树的节点个数//L1-R1代表中序遍历左子树, L2-R2代表后续遍历左子树,后续遍历通过cnt确定 r->left = createTree(L1, p-1, L2, L2+cnt-1);   r->right = createTree(p+1, R1, L2+cnt, R2-1);return r;
}void levelOrder(Node* root) {queue<Node*>q;q.push(root);bool output = false;while(!q.empty()) {Node*t = q.front();q.pop();if(output) putchar(' ');printf("%d", t->data);output = true;if(t->left != nullptr) q.push(t->left);if(t->right != nullptr) q.push(t->right); }
}int main() {int n; cin >> n;for(int i = 0; i < n; i++) cin >> postOrder[i];for(int i = 0; i < n; i++) cin >> inOrder[i];Node *root = createTree(0, n-1, 0, n-1);levelOrder(root);return 0;
}

耗时:


                ——做自己的守护神

【详细注解】1020 Tree Traversals (25 分)相关推荐

  1. 1020 Tree Traversals (25 分) 【难度: 中 / 知识点: 哈希表建树 遍历树】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805485033603072 第一步: 建树 第二步: 遍历树 #incl ...

  2. 1020. Tree Traversals (25)

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

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

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

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

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

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

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

  6. 1020 月饼 (25 分)

    1020 月饼 (25 分) 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. 注意 ...

  7. PAT 1020. Tree Traversals

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

  8. 1020 Tree Traversals

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

  9. 1086 Tree Traversals Again

    1. 这题的核心部分是,根据二叉树的先序序列和中序序列求后序序列.等于是在1020 Tree Traversals这一题的基础上,把怎么得到先序序列和中序序列的难度加大了,不是直接给出,而是要曲折一点 ...

最新文章

  1. CMake编译Makefile
  2. C语言从0到1·源程序,源文件,目标文件之间的关系
  3. java eclipse 注释模板_Eclipse Java注释模板设置详解
  4. electron 读取文件夹内容_如何使用Electron Framework选择,读取,保存,删除或创建文件...
  5. 一笔订单,但是误付了两笔钱!这种重复付款异常到底该如何解决?
  6. 线性表的链表存储实现
  7. devexpress 打印一个form界面_通过回车键提交form表单时,你是否注意过这些问题?...
  8. 速成pytorch学习——8天损失函数
  9. 2021牛客暑期多校训练营1, 签到题DFBG
  10. 《Ray Tracing in One Weekend》——Chapter 1: Output an image
  11. 解决向github提交代码不用输入帐号密码
  12. React Native npm镜像安装命令
  13. Python基础--02
  14. Django 中针对基于类的视图添加 csrf_exempt
  15. jena java_对Jena的简单理解和一个例子
  16. BugkuCTF_Web——“秋名山老司机”、“速度要快”、“cookies欺骗”
  17. 两个ESP8266一个作为服务器一个作为客户端实现互相通讯
  18. 今日全国油价查询2022-03-08
  19. Node如何处理模块之间的关系
  20. c语言中的/和%表示什么意思

热门文章

  1. Redis 特殊数据类型 :Geospatial、Hyperloglog、Bitmap
  2. python爬虫 隐藏身份及设置代理
  3. 音视频技术开发周刊 | 185
  4. 从上海到旧金山,2021 LiveVideoStackCon回归上海
  5. 音视频技术开发周刊 | 138
  6. 窥见C++11智能指针
  7. go context之WithCancel的使用
  8. 使用idea编写SparkStreaming消费kafka中的数据【小案例】(四)
  9. leetcode 287. Find the Duplicate Number | 287. 寻找重复数(判断链表是否有环,并找到环的起点)
  10. 网络与IO知识扫盲(三):从系统调用的角度,剖析 Socket 的连接过程、BIO 的连接过程