1086 Tree Traversals Again——PAT甲级真题
1086 Tree Traversals Again
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<=30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: “Push X” where X is the index of the node being pushed onto the stack; or “Pop” meaning to pop one node from the stack.
Output Specification:
For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.
Sample Input:
6
Push 1
Push 2
Push 3
Pop
Pop
Push 4
Pop
Pop
Push 5
Push 6
Pop
Pop
Sample Output:
3 4 2 6 5 1
题目大意:用栈来模拟以可二叉树的中序遍历,让你求出这棵二叉树的后序遍历。
大致思路:由题意可知二叉树的输入顺序为二叉树的前序遍历,然后我们借助一个栈来得出二叉树的后续遍历。进行一次Push操作我们就像栈中存一个元素,Pop操作我们就返回栈顶元素并将元素存入数组中。最后得到二叉树的中序遍历序列。
#include <bits/stdc++.h>
using namespace std;
const int N = 65;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
int n;
TreeNode root[N];
vector<int> preorder, inorder, postorder; //先序序列,中序序列
void newNode(int x) {
root[x].val = x;
root[x].left = root[x].right = -1;
}
//给出了中序和先序构建二叉树
TreeNode* create(int preL, int preR, int inL, int inR) {
if (preL > preR) return -1;
TreeNode* root = new TreeNode(preorder[preL]);
int k;
for (k = inL; k <= inR; k++) {
if (inorder[k] == preorder[preL]) break;
}
int numL = k - inL;
root->left = create(preL + 1, preL + numL, inL, k - 1);
root->right = create(preL + numL + 1, preR, k + 1, inR);
return root;
}
void postvisit(TreeNode* root) {
if (node == -1) return;
postvisit(root->left);
postvisit(root->right);
postorder.push_back(root->val);
}
int main() {
scanf("%d", &n);
//先将每个节点初始化
stack<int> tmp;
string op;
int id;
while(n--) {
cin >> op;
if (op == "Push") {
cin >> id;
tmp.push(id);
preorder.push_back(id);
}
else {
inorder.push_back(tmp.top());
tmp.pop();
}
}
create(0, n - 1, 0, n - 1);
TreeNode* root = create(0, n - 1, 0 , n - 1);
postvisit(root);
for (int i = 0; i < postorder.size(); i++) {
cout << postorder[i];
if (i != postorder.size() - 1) cout << " ";
else cout << endl;
}
return 0;
}
1086 Tree Traversals Again——PAT甲级真题的更多相关文章
- PAT 甲级真题题解(63-120)
2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...
- 1020 Tree Traversals——PAT甲级真题
1020 Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Give ...
- PAT 甲级真题题解(1-62)
准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format 模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...
- PAT 甲级真题
1019. General Palindromic Number 题意:求数N在b进制下其序列是否为回文串,并输出其在b进制下的表示. 思路:模拟N在2进制下的表示求法,“除b倒取余”,之后判断是否回 ...
- 1080 Graduate Admission——PAT甲级真题
1080 Graduate Admission--PAT甲级练习题 It is said that in 2013, there were about 100 graduate schools rea ...
- 1102 Invert a Binary Tree——PAT甲级真题
1102 Invert a Binary Tree The following is from Max Howell @twitter: Google: 90% of our engineers us ...
- PAT甲级真题及训练集
正好这个"水水"的C4来了 先把甲级刷完吧.(开玩笑-2017.3.26) 这是一套"伪题解". wacao 刚才登出账号测试一下代码链接,原来是看不到..有空 ...
- PAT 甲级真题题解(121-155)
1121 Damn Single 模拟 // 1121 Damn Single #include <map> #include <vector> #include <cs ...
- PAT甲级真题 A1025 PAT Ranking
题目概述:Programming Ability Test (PAT) is organized by the College of Computer Science and Technology o ...
随机推荐
- 【洛谷 p3372】模板-线段树 1(数据结构--线段树)
题目:已知一个数列,你需要进行下面两种操作:1.将某区间每一个数加上x:2.求出某区间每一个数的和. 解法:如题,模版题.需要加上 lazy 标记,也就是我的 upd.lazy 标记的思路就是对一个结 ...
- hdu5135 Little Zu Chongzhi's Triangles
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) Total Submissi ...
- rabbitMq学习一
1.安装rabbitmq Windows下安装 首先,我们访问官网[https://www.rabbitmq.com/],点击Get Started. 选择下载安装,由于RabbitMQ使用Erlan ...
- MySql 执行 DELETE/UPDATE时,报 Error Code: 1175错误
MySql 执行 DELETE FROM Table 时,报 Error Code: 1175. You are using safe update mode and you tried to upd ...
- JVM调优参数、方法、工具以及案例总结
这种文章挺难写的,一是JVM参数巨多,二是内容枯燥乏味,但是想理解JVM调优又是没法避开的环节,本文主要用来总结梳理便于以后翻阅,主要围绕四个大的方面展开,分别是JVM调优参数.JVM调优方法(流程) ...
- 国产网络损伤仪 SandStorm -- 只需要拖拽就能删除链路规则
国产网络损伤仪SandStorm可以模拟出带宽限制.时延.时延抖动.丢包.乱序.重复报文.误码.拥塞等网络状况,在实验室条件下准确可靠地测试出网络应用在真实网络环境中的性能,以帮助应用程序在上线部署前 ...
- ElasticSearch 集群 & 数据备份 & 优化
ElasticSearch 集群相关概念 ES 集群颜色状态 ①. - 红色:数据都不完整 ②. - 黄色:数据完整,但是副本有问题 ③. - 绿色:数据和副本全都没有问题 ES 集群节点类型 ①. ...
- iTerm2终端工具在Mac OS上使用详解
一.概述 因个人工作需要,使用终端工具进行运维和开发工作,但是Mac OS 自带的终端工具使用堡垒机登录配置不了,而且使用CRT等终端工具每次登录堡垒机都需要配置密码,操作起来很麻烦.一直想找一款终端 ...
- boomworks 1999~2009
大众软件 PC定时执行专家 4.0 (PCTaskTimer) - 功能强大.简单易用的定时执行软件.具有功能多.体积小.消耗资源少的特点. 超级网际搜索(SuperSearch) - 免费.快速.高 ...
- c++ cin 读入txt的问题
源程序 #include <iostream> using namespace std; struct Stack { int tos; int stackarray[1000]; }; ...