03-树3 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.
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 分)的更多相关文章
- 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 ...
- PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习
1086 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-recu ...
- 数据结构课后练习题(练习三)7-5 Tree Traversals Again (25 分)
7-5 Tree Traversals Again (25 分) An inorder binary tree traversal can be implemented in a non-recu ...
- 【PAT甲级】1086 Tree Traversals Again (25 分)(树知二求一)
题意:输入一个正整数N(<=30),接着输入2*N行表示栈的出入(入栈顺序表示了二叉搜索树的先序序列,出栈顺序表示了二叉搜索树的中序序列),输出后序序列. AAAAAccepted code: ...
- A1020 Tree Traversals (25 分)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- PAT A1020 Tree Traversals (25 分)——建树,层序遍历
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- 1020 Tree Traversals (25 分)(二叉树的遍历)
给出一个棵二叉树的后序遍历和中序遍历,求二叉树的层序遍历 #include<bits/stdc++.h> using namespace std; ; int in[N]; int pos ...
- 03-树2. Tree Traversals Again (25)
03-树2. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...
- 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 ...
随机推荐
- windows10结束进程
.net winfrom 程序关于结束进程触发事件 在任务管理器中有进程.详细信息栏 在进程栏对应用程序结束任务,会触发应用程序窗体的FormClosed事件 在详细信息栏对应用程序结束任务,不会触发 ...
- 不用Pageant告别Pageant Windows10下TortoiseGit和Git配置使用同一SSH密钥
关于Git使用SSH免密连接参考:https://blog.csdn.net/qq_32786873/article/details/80570788 关于Windows10下TortoiseGit使 ...
- 树莓派Raspbian系统密码
树莓派Raspbian系统密码 树莓派Raspbian系统默认登录用户名为pi,该账户默认密码是raspberry(可在raspi-config中修改). 树莓派的Raspbian系统root用户默认 ...
- 全程实操cdh5.14.4中集成安装kylin2.4.1与使用测试
在cdh5.14.4安装完成并排错完成的情况下,进行如下kylin安装操作: 1.实验环境 三台CentOS 7主机,IP地址 192.168.43.129 cm1 192.168.43.130 cm ...
- CSS中:和::
一个冒号是伪类,两个冒号是伪元素 伪类可以独立于文档的元素来分配样式,且可以分配给任何元素,逻辑上和功能上类类似,但是其是预定义的.不存在于文档树中且表达方式也不同,所以叫伪类.伪元素所控制的内容和一 ...
- Mysql 游标初识
MySql 游标初识 认识 游标(cursor), 按字面意思可理解为, 游动的标识, 或者叫做"光标", 这样更容易理解. 就好比现有一张表存储了n行记录, 然后我想每次取出一行 ...
- Golang: 解析JSON数据之二
上次我们介绍了 Go 语言中序列化和反序列化 JSON 数据的两个方法 Marshal() 和 Unmarshal(),并以示例演示了它们的用法. 我们在例子中看到,需要事先声明好对应的结构体,才能跟 ...
- Django 之 CBV
Django 中有两种编写方式,FBV 和 CBV,那么什么是 FBV,CBV 又是什么呢? 一.什么是 CBV FBV(function base views) 就是在视图里使用函数处理请求(常见) ...
- Prometheus(五):Prometheus+Alertmanager 配置企业微信报警
此处默认已安装Prometheus服务,服务地址:192.168.56.200 一.设置企业微信 1.1.企业微信注册(已有企业微信账号请跳过) 企业微信注册地址:https://work.weix ...
- SHOI2017 分手是祝愿
分手是祝愿 有