PAT_A1086#Tree Traversals Again
Source:
Description:
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.
Figure 1
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2 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
Keys:
Code:
/*
time: 2019-06-30 14:34:48
problem: PAT_A1086#Tree Traversals Again
AC: 24:16 题目大意:
给出中序遍历的出栈和入栈操作,打印后序遍历 基本思路:
模拟二叉树的遍历过程,Push就是存在子树,Pop就是空子树
*/
#include<cstdio>
#include<string>
#include<iostream>
using namespace std;
int n; void InOrder()
{
string op;
int data;
cin >> op;
if(op == "Push")
scanf("%d", &data);
else
return;
static int pt=;
InOrder();
InOrder();
printf("%d%c", data, ++pt==n?'\n':' ');
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE scanf("%d", &n);
InOrder(); return ;
}
PAT_A1086#Tree Traversals Again的更多相关文章
- Tree Traversals
Tree Traversals 原题链接 常见的二叉树遍历的题目,根据后序遍历和中序遍历求层次遍历. 通过后序遍历和中序遍历建立起一棵二叉树,然后层序遍历一下,主要难点在于树的建立,通过中序遍历和后序 ...
- HDU 1710 二叉树的遍历 Binary Tree Traversals
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- hdu1710(Binary Tree Traversals)(二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU1710Binary Tree Traversals
HDU1710Binary Tree Traversals 题目大意:给一个树的前序遍历和中序遍历,要求输出后序遍历. (半年前做这道题做了两天没看懂,今天学了二叉树,回来AC了^ ^) 首先介绍一下 ...
- HDU-1701 Binary Tree Traversals
http://acm.hdu.edu.cn/showproblem.php?pid=1710 已知先序和中序遍历,求后序遍历二叉树. 思路:先递归建树的过程,后后序遍历. Binary Tree Tr ...
- 03-树2. Tree Traversals Again (25)
03-树2. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...
- HDU 1710-Binary Tree Traversals(二进制重建)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- PAT1086:Tree Traversals Again
1086. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- Binary Tree Traversals(HDU1710)二叉树的简单应用
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
随机推荐
- 把sublime添加到右键快捷菜单
方法一: 新建sublime_addright.reg文件 编辑后双击打开就OK 括号内是sublime安装路径:使用的时候去掉括号 open sublimeText3 是提示文字 Windows R ...
- stl+数论——1247D
其实也不算很难想,每个元素质因子分解后的p^c的p和c用pair的形式存在每个元素vector里 要去前面找一个数使得所有指数相加是k的倍数,那么把vector里的所有c 模 k,然后去找前面互补的数 ...
- flutter 卡在Running Gradle task 'assembleDebug'...
Android项目运行时出错 卡在Initializing gradle… 运行时会卡在Initializing gradle..., 此时因为Android项目会用到Gradle, 如果没有FQ,下 ...
- fatal error C1189: #error : "No Target Architecture" 解决办法一
在编译程序的时候发现报这个错误,在网上看到很多文章,说设置include路径,lib目录等等,都没有解决.最后调整了以下include文件的顺序,问题解决了.例如 从头文件a.h中截取的一段 type ...
- Amazon Linux AMI 2015.09 (HVM)平台搭建lamp
更新yum yum update 安装Apache: yum install -y httpd 安装完之后,重新启动 service httpd restart 将Apache设置为开机启动 chkc ...
- smali与baksmali用法-基于2.2.2版本
下载地址:https://bitbucket.org/JesusFreke/smali/downloads/?tab=downloads 反编译dex java -jar baksmali-2.2.1 ...
- PAT_A1130#Infix Expression
Source: PAT A1130 Infix Expression (25 分) Description: Given a syntax tree (binary), you are suppose ...
- 面试总结【css篇】- css选择器以及优先级
优先(优先级为): !important > 内联样式 > #id > .class > tag > * > 继承 > 默认 . 当选择器的权重相同时,它将 ...
- python全栈开放实践第三版第一章的练习题完成情况
练习题: 1.简述编译型与解释型语言的区别,且分别列出你知道哪些语言属于编译型,哪些数以解释型.1 编译型:只须编译一次就可以把源代码编译成机器语言,后面的执行无须重新编译,直接使用之前的编译结果就可 ...
- C语言清空指针
#include <stdio.h> int main() { /********************************************* * * %d int * %f ...