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. ajax 执行成功以后返回的数据走的是error方法而不是success方法的问题

    今天在一个功能的时候发现写的ajax的方法执行后台代码成功后返回前台时执行的是error方法而不是success方法,代码如下 jQuery('#form').ajaxSubmit({ type: & ...

  2. WebAPI 身份认证解决方案——Phenix.NET企业应用软件快速开发平台.使用指南.21.WebAPI服务(一)

    21   WebAPI服务 ASP.NET Web API,是微软在.NET Framework 4.5上推出的轻量级网络服务框架,虽然作为ASP.NET MVC 4的一部分,但却是一套全新的.独立的 ...

  3. rsyslog详解实战和避坑

    目标是要把线上环境的debug日志及集中化收集起来,一方面是方便开发调试:一方面是避免直接到线上环境查看,存在安全隐患. 常用可选方案: rsyslog发送端 + rsyslog接收端: 直接存在接收 ...

  4. Eureka设计原理

    1. Eureka设计原理 1.1. 前言 目前我越来越关注技术原理层面的东西,开始考虑中间件设计背后,要考虑哪些因素,为什么要这样设计,有什么优化的地方,这次来讨论Eureka 1.2. 设计问题 ...

  5. gulp与webpack的区别?是一种工具吗?

    问:gulp和webpack什么关系,是一种东西吗?可以只用gulp,不用webpack吗 或者反过来?有什么区别? 答:gulp是工具链.自动化构建工具,可以配合各种插件,我们不用再做机械重复的工作 ...

  6. Android studio module生成jar包,module中引用的第三方库没有被引用,导致java.lang.NoClassDefFoundError错误。

    android studio 创建了一个Module生成jar包,这个module中有引用一些第三方的类库,比如 gson,volley等. 但是生成的jar包里,并没有将gson,volley等第三 ...

  7. Servlet HttpServletResponse对象、HttpServletRequest对象

    HttpServletResponse对象(response)的常用方法 setCharacterEncoding("utf-8")    //设置响应的编码字符集 setCont ...

  8. Nexus6p手机root和安装xposed

    进行root前需要两个前提条件 解锁OEM 进入开发者选项:设置-〉关于-〉一直点版本号会出现,usb调试打开 手机连接pc命令行输入: adb reboot bootloader 进入bootloa ...

  9. Java集合学习(4):HashTable

    一.概述 和HashMap一样,Hashtable也是一个散列表,它存储的内容是键值对. Hashtable在Java中的定义为: public class Hashtable<K,V> ...

  10. Vue.js学习-组件注册与使用

    Vue.js学习文档 地址:https://cn.vuejs.org/v2/guide/ 关于自定义组件注册: 建议将<script></script>放在body标签之后 H ...