PAT日志 1086
顽强的小白
1086 Tree Traversals Again (25 分)
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
题目解析
用栈的形式来表示二叉树,这里就会发现一个有趣的现象,所有入栈的操作序列就是先序遍历,而所有出栈的序列就是中序遍历。发现这个现象后就和上一道二叉树的题目一样了,这次是给出中序和先序求后序遍历。
代码实现
#include <cstdio>
#include <algorithm>
#include <stack>
#include <cstring>
using namespace std;
const int maxn=35;
int n;
int in[maxn];
int pre[maxn];
int post[maxn];
struct node{int data;node* l;node* r;
};
node* create(int inL,int inR,int preL,int preR){if(inL>inR) return NULL;node* root=new node;root->data=pre[preL];int k;for(k=inL;k<=inR;++k){if(in[k]==pre[preL])break;}int left=k-inL;root->l=create(inL,k-1,preL+1,preL+left);root->r=create(k+1,inR,preL+left+1,preR);return root;
}
int cnt=0;
void postOrder(node* root){if(root==NULL) return;postOrder(root->l);postOrder(root->r);printf("%d",root->data);cnt++;if(cnt<n) printf(" ");}
int main(){stack<int> s;scanf("%d",&n);char mk[6];int j=0,k=0,x;for(int i=0;i<n*2;++i){scanf("%s",mk);if(strcmp(mk,"Push")==0){scanf("%d",&x);pre[j++]=x;s.push(x);}else{in[k++]=s.top();s.pop();}}node* root=create(0,n-1,0,n-1);postOrder(root);return 0;
}
PAT日志 1086相关推荐
- PAT甲级1086 Tree Traversals Again:[C++题解]二叉树中序序列、栈、求后序遍历
文章目录 题目分析 题目链接 题目分析 分析: 给定栈模拟的二叉树的中序序列. 我们可以发现一些性质: 1 第一个值是根结点. 2 对于所有的push操作,如果上一个是push,该结点就是上一个结点的 ...
- PAT初级1086就不告诉你(C++)
PAT初级1086就不告诉你(C++) 题目: 代码: 结果: PLUS(搬运): 题目: 代码: # include<iostream> # include<cstring> ...
- PAT日志 1035
顽强的小白 1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords ...
- PAT乙级1086 就不告诉你(C语言)
1086 就不告诉你 (15 分) 做作业的时候,邻座的小盆友问你:"五乘以七等于多少?"你应该不失礼貌地围笑着告诉他:"五十三."本题就要求你,对任何一对给定 ...
- PAT乙级 1086 就不告诉你 (附测试点1,2排查及用例)
对任何一对给定的正整数,倒着输出它们的乘积. 输入格式: 输入在第一行给出两个不超过 1000 的正整数 A 和 B,其间以空格分隔. 输出格式: 在一行中倒着输出 A 和 B 的乘积. 输入样例: ...
- PAT日志 1055
顽强的小白 1055 The World's Richest (25 分) Forbes magazine publishes every year its list of billionaires ...
- PAT日志 1028
顽强的小白 1028 List Sorting (25 分) Excel can sort records according to any column. Now you are supposed ...
- PAT日志 1095
顽强的小白 1095 Cars on Campus (30 分) Zhejiang University has 8 campuses and a lot of gates. From each ga ...
- PAT 乙级 1086 python
题目链接:https://pintia.cn/problem-sets/994805260223102976/problems/1038429065476579328 题目如下: 做作业的时候,邻座的 ...
最新文章
- delphi 使用UDP收发数据
- UITableViewCell 添加长按手势
- 在看完《Programming in Lua》之后
- 工控设备 如何将数据发送到串口_嵌入式无风扇工控机在水质监测系统中的应用...
- Java面试中常问的Spring方面问题
- 在Linux系统下载email,LINUX下安装U-MAIL邮件系统
- linux 下 eclipse 开发环境的搭建
- javascript MouseEvent对象
- .NET之EntityFramework框架运用
- Spring Cloud Config分布式配置中心(学习总结)
- 新浪微博开放平台中的Redis实践
- 第二篇:Spring Cloud Eureka 服务注册+发现
- mysql账号认证_浅谈MySQL用户账号认证方式
- 粉笔公考——方法精讲——资料分析
- 事实表与维度表的区别与关系
- 彩色空间(Color Space)
- NFC framework introduce(一)
- FGF21 类似物 PF-05231023 改善糖尿病并发症
- tar压缩排除某个文件夹
- 五一北京周边出行参考