顽强的小白

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相关推荐

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

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

  2. PAT初级1086就不告诉你(C++)

    PAT初级1086就不告诉你(C++) 题目: 代码: 结果: PLUS(搬运): 题目: 代码: # include<iostream> # include<cstring> ...

  3. PAT日志 1035

    顽强的小白 1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords ...

  4. PAT乙级1086 就不告诉你(C语言)

    1086 就不告诉你 (15 分) 做作业的时候,邻座的小盆友问你:"五乘以七等于多少?"你应该不失礼貌地围笑着告诉他:"五十三."本题就要求你,对任何一对给定 ...

  5. PAT乙级 1086 就不告诉你 (附测试点1,2排查及用例)

    对任何一对给定的正整数,倒着输出它们的乘积. 输入格式: 输入在第一行给出两个不超过 1000 的正整数 A 和 B,其间以空格分隔. 输出格式: 在一行中倒着输出 A 和 B 的乘积. 输入样例: ...

  6. PAT日志 1055

    顽强的小白 1055 The World's Richest (25 分) Forbes magazine publishes every year its list of billionaires ...

  7. PAT日志 1028

    顽强的小白 1028 List Sorting (25 分) Excel can sort records according to any column. Now you are supposed ...

  8. PAT日志 1095

    顽强的小白 1095 Cars on Campus (30 分) Zhejiang University has 8 campuses and a lot of gates. From each ga ...

  9. PAT 乙级 1086 python

    题目链接:https://pintia.cn/problem-sets/994805260223102976/problems/1038429065476579328 题目如下: 做作业的时候,邻座的 ...

最新文章

  1. delphi 使用UDP收发数据
  2. UITableViewCell 添加长按手势
  3. 在看完《Programming in Lua》之后
  4. 工控设备 如何将数据发送到串口_嵌入式无风扇工控机在水质监测系统中的应用...
  5. Java面试中常问的Spring方面问题
  6. 在Linux系统下载email,LINUX下安装U-MAIL邮件系统
  7. linux 下 eclipse 开发环境的搭建
  8. javascript MouseEvent对象
  9. .NET之EntityFramework框架运用
  10. Spring Cloud Config分布式配置中心(学习总结)
  11. 新浪微博开放平台中的Redis实践
  12. 第二篇:Spring Cloud Eureka 服务注册+发现
  13. mysql账号认证_浅谈MySQL用户账号认证方式
  14. 粉笔公考——方法精讲——资料分析
  15. 事实表与维度表的区别与关系
  16. 彩色空间(Color Space)
  17. NFC framework introduce(一)
  18. FGF21 类似物 PF-05231023 改善糖尿病并发症
  19. tar压缩排除某个文件夹
  20. 五一北京周边出行参考

热门文章

  1. Diagonal distance in 23 dimensions
  2. 渗透测试-ThinkPHP框架漏洞总结
  3. 借助 Material Design,帮助您打造更好的无障碍应用 (中篇)
  4. Android 7.0真实上手体验
  5. IObit Uninstaller 8.0中文专业版-软件卸载神器(内含注册码)
  6. 2022考研数学-概率论教程
  7. 二个数据库之间怎么同步
  8. iZON:有苹果,就能让天涯变咫尺
  9. Facebook Android集成和开发
  10. 如何离线安装python包