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
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
const int maxn = ; struct Node
{
int data;
Node *lchild, *rchild;
}; int in[maxn] = {}, pre[maxn] = {};
int num = ; Node* createTree(int preL, int preR, int inL, int inR);
void postOrder(Node *root,int n); int main()
{
int n;
scanf("%d",&n); int x;
int preIndex = , inIndex = ;
char str[];
stack<int> s; for (int i = ; i < *n; i++)
{
getchar();
scanf("%s",str);
if ( == strcmp(str,"Push"))
{
scanf("%d",&x);
s.push(x);
pre[preIndex++] = x;
}
else
{
x = s.top();
s.pop();
in[inIndex++] = x;
}
} Node *root = createTree(,n-,,n-);
postOrder(root,n);
return ;
} Node* createTree(int preL, int preR, int inL, int inR)
{
if (preL > preR)
{
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 numLeft = k - inL;
root->lchild = createTree(preL+, preL+numLeft, inL, k-);
root->rchild = createTree(preL+numLeft+, preR, k+, inR);
return root;
} void postOrder(Node *root,int n)
{
if (root == NULL)
{
return;
}
postOrder(root->lchild,n);
postOrder(root->rchild,n);
printf("%d",root->data); num++;
if (num < n)
{
printf(" ");
}
}

03-树3 Tree Traversals Again (25 分)的更多相关文章

  1. PTA 03-树3 Tree Traversals Again (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/667 5-5 Tree Traversals Again   (25分) An inor ...

  2. PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习

    1086 Tree Traversals Again (25分)   An inorder binary tree traversal can be implemented in a non-recu ...

  3. 数据结构课后练习题(练习三)7-5 Tree Traversals Again (25 分)

    7-5 Tree Traversals Again (25 分)   An inorder binary tree traversal can be implemented in a non-recu ...

  4. 【PAT甲级】1086 Tree Traversals Again (25 分)(树知二求一)

    题意:输入一个正整数N(<=30),接着输入2*N行表示栈的出入(入栈顺序表示了二叉搜索树的先序序列,出栈顺序表示了二叉搜索树的中序序列),输出后序序列. AAAAAccepted code: ...

  5. A1020 Tree Traversals (25 分)

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  6. PAT A1020 Tree Traversals (25 分)——建树,层序遍历

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  7. 1020 Tree Traversals (25 分)(二叉树的遍历)

    给出一个棵二叉树的后序遍历和中序遍历,求二叉树的层序遍历 #include<bits/stdc++.h> using namespace std; ; int in[N]; int pos ...

  8. 03-树2. Tree Traversals Again (25)

    03-树2. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...

  9. 03-树3. Tree Traversals Again (25)将先序遍历和中序遍历转为后序遍历

    03-树3. Tree Traversals Again (25) 题目来源:http://www.patest.cn/contests/mooc-ds/03-%E6%A0%913 An inorde ...

随机推荐

  1. 动软软件 生成 实体类模板(EnterpriseFrameWork框架)

    1.废话不多说,直接上效果图 . 2 .动软模板代码 <#@ template language="c#" HostSpecific="True" #&g ...

  2. Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1

    可以看出是 maven-surefire-plugin:2.18.1 插件问题,在网上寻找解决方案如下: <plugin> <groupId>org.apache.maven. ...

  3. English--分词短语

    English|分词短语 现在开始讲解分词短语的内容.在英语的语法世界里面,想要将句子写的漂亮,分词短语,你值得拥有! 前言 目前所有的文章思想格式都是:知识+情感. 知识:对于所有的知识点的描述.力 ...

  4. 基于OpenGL三维软件开发

    实验原理: OpenGL在MFC下编程原理---- Windows操作系统对OpenGL的支持 在Windows下用GDI作图必须通过设备上下文(DeviceContext简写DC)调用相应的函数:用 ...

  5. box-shadow 模糊半径与扩展半径

    关于box-shadow的基本用法参阅CSS3 box-shadow一章节. 此属性用来设置元素的阴影效果,语法结构如下: box-shadow:h-shadow v-shadow blur spre ...

  6. Linux配置swap

    根据自己的物理内存分配合适的swap大小 下面是合适的配置 物理内存 交换分区(swap) <=4G 至少2G 4-16G 至少4G 16G-64 至少8G 下面是操作步骤 1.首先查看我们的内 ...

  7. es数据二次开发统计展示

    案例1 在es查询中按照多列分组的时候 分组列的count值会越来越少 es默认隐藏了没有被分组匹配到的记录数  需要在查询的时候开启 2.开启显示没有被分组成功的记录 分组成功的记录加上分组miss ...

  8. Zebra-打印特殊字符

    Zebra在打印一些特殊的字符时,会出异常. 在要打印的字符串前加  ^FH  然后将字符换成 ASCii编码或utf-8编码的16进制,在前面加_,如D094写成_DO_94 查看字符的编码 htt ...

  9. linux 常用工具记录及简介

    前言 linuxz虽然各种软件的生态还比较差,但是大势所趋,早晚都是要用的.记录下自己常用的软件,要是那天系统崩了重装也舒服点 编程工具 pycharm专业版(社区版也能用,只是用惯了专业版) * 下 ...

  10. web服务器-nginx

    一.nginx之前 同步与异步: 同步与异步的重点在消息通知的方式上,也就是调用结果的通知方式不同. 同步:当一个同步调用发出去后,调用者要一直等待调用的结果通知后,才能进行后续的操作. 异步:当一个 ...