PAT 1086 Tree Traversals Again
室友在做于是也做一发,跟已知两种遍历序列还原二叉树的思路类似,感觉PAT上的题目跟书本知识靠的近一些
#include <iostream> #include <cstdio> #include <vector>using namespace std;void print_data(const vector<char> &as, const vector<int> &ns) {int len = ns.size();for (int i=0; i<len; i++) {if (as[i] == 'i') {cout<<"push "<<ns[i]<<endl;} else {cout<<"pop"<<endl;}} }void build_postorder(vector<char> &actions, vector<int> &nums, int start, int end, vector<int> &postorder) {if (start >= end) return;if (actions[start] != 'i') return;// root node postorder.push_back(nums[start]);// now try to find the seperator idx of // action(must be a pop pair wise with root node push) between two sub treeint pushs = 1;int idx = start + 1;while (pushs != 0 && idx < end) {if (actions[idx] == 'i') {pushs++;} else {pushs--;}idx++;}// right sub tree build_postorder(actions, nums, idx, end, postorder);// left sub treebuild_postorder(actions, nums, start + 1, idx, postorder); } int main() {int N, len;scanf("%d", &N);len = N * 2;char buf[10];int num;vector<char> actions(2 * N, '\0');vector<int> nums(2 * N, 0);// read in datafor (int i=0; i<len; i++) {scanf("%s", buf);if (buf[3] == 'h') {scanf("%d", &num);nums[i] = num;actions[i] = 'i'; // push, input} else {actions[i] = 'o'; // pop, output }}vector<int> postorder;build_postorder(actions, nums, 0, len, postorder);if (postorder.size() > 0){// print_data(actions, nums);for (int i=N - 1; i>=1; i--) {printf("%d ", postorder[i]);}printf("%d", postorder[0]);}return 0; }
转载于:https://www.cnblogs.com/lailailai/p/3967525.html
PAT 1086 Tree Traversals Again相关推荐
- PAT甲级1086 Tree Traversals Again:[C++题解]二叉树中序序列、栈、求后序遍历
文章目录 题目分析 题目链接 题目分析 分析: 给定栈模拟的二叉树的中序序列. 我们可以发现一些性质: 1 第一个值是根结点. 2 对于所有的push操作,如果上一个是push,该结点就是上一个结点的 ...
- 1086 Tree Traversals Again (25分)
1 题目 1086 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-r ...
- 1086 Tree Traversals Again
1. 这题的核心部分是,根据二叉树的先序序列和中序序列求后序序列.等于是在1020 Tree Traversals这一题的基础上,把怎么得到先序序列和中序序列的难度加大了,不是直接给出,而是要曲折一点 ...
- 【PAT (Advanced Level) Practice】1086 Tree Traversals Again (25 分)
众所周知只有前序遍历和后序遍历是不可能推出中序遍历的,所以我就不废话了 由树的前序遍历和中序遍历可以推后序遍历 由树的中序遍历和后序遍历可以推前序遍历 #include <cstdio> ...
- PAT 1020. Tree Traversals
题目:http://pat.zju.edu.cn/contests/pat-a-practise/1020 题解: 题意就是给一个二叉树的后序和中序,然后确定这棵二叉树,然后层序输出这棵树. 主要思路 ...
- 1086 Tree Traversals Again (25 分)【一般 / 建树 树的遍历】
https://pintia.cn/problem-sets/994805342720868352/problems/994805380754817024 首先要知道的是: 入栈顺序就是树的前序遍历的 ...
- PAT甲级1020 Tree Traversals:[C++题解]树的遍历、由中序序列和后序序列递归建树
文章目录 题目分析 题目链接 题目分析 题意重述:给定一棵二叉树的后序遍历序列和中序遍历序列,让求层次遍历的序列. 分析: 后序遍历:先 左子树.右子树 ,最后再遍历根结点. 中序遍历:先左子树,再根 ...
- 1020. Tree Traversals (25)
题目链接:http://www.patest.cn/contests/pat-a-practise/1020 题目: 1020. Tree Traversals (25) 时间限制 400 ms 内存 ...
- 03-树3 Tree Traversals Again
题目 An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For exam ...
- hdu 1710 Binary Tree Traversals (二叉树)
1 /********************************************************** 2 题目: Binary Tree Traversals(hdu 1710) ...
最新文章
- Style Report中报表组件的使用场景简析
- 深度学习的实用层面 —— 1.9 正则化输入
- ubuntu 安装使用多版本opencv
- Python 爬虫---(7) Python3网络爬虫快速入门实战解析
- Madagascar的自定义浮点型函数--取整函数
- android 中LayoutInflater 的使用
- ex is not shell_我使用过的Linux命令之exit - 退出当前shell
- 值得思考:过去的中国大学
- linux系统32位镜像下载,深度Linux Deepin镜像下载
- 技术揭秘QQ空间”自动转发不良信息
- 勘测定界坐标交换格式文本文件转换成 shapefile 格式
- matlab基波有效值,基波有效值
- NFT成为社交地位的新符号象征
- 7-6 统计大写辅音字母
- 北京地区的图像处理公司
- 计算机操作系统-磁盘存储器
- Css Reset -Css样式重置
- 计算机等级考试第一次报什么,计算机等级考试一年是考两次,3月和9月各一次 如果第一次没有考过第二次考要再报名缴费吗?...
- Sorry, The number you dialed is power off.
- 巨头“围攻”之下,新氧医美能否“破局”?