题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/667

5-5 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 NN (\le 30≤30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to NN). Then 2N2Nlines 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



这道题是根据输入算出前序遍历和中序遍历,不过没有直接建树去做后序,而是参照前两者遍历的序列,递归分解算了后续出来
/*
评测结果
时间 结果 得分 题目 编译器 用时(ms) 内存(MB) 用户
2017-07-08 16:00 答案正确 25 5-5 gcc 14 1
测试点结果
测试点 结果 得分/满分 用时(ms) 内存(MB)
测试点1 答案正确 12/12 14 1
测试点2 答案正确 4/4 2 1
测试点3 答案正确 4/4 2 1
测试点4 答案正确 1/1 13 1
测试点5 答案正确 4/4 2 1 */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXLEN 50
struct stack{
int data[MAXLEN];
int top;
}; typedef struct stack *ptrStack; int printcount=0;
int length; ptrStack CreateStack()
{
ptrStack temp;
temp=(ptrStack)malloc(sizeof(struct stack));
temp->top=0;
return temp;
} void Push(ptrStack s,int item)
{
s->data[++(s->top)]=item;
} int Pop(ptrStack s)
{
return s->data[(s->top)--];
} void DestroyStack(ptrStack s)
{
free(s);
} void Process(int preOrder[],int inOrder[],int preStart,int preEnd,int inStart,int inEnd)
{
int i,root,mid,leftsize,rightsize;
int nextLeftpreStart,nextLeftpreEnd,nextLeftinStart,nextLeftinEnd;
int nextRightpreStart,nextRightpreEnd,nextRightinStart,nextRightinEnd;
if(preStart==preEnd){
printf("%d",preOrder[preStart]);
printcount++;
if (printcount!=length)
putchar(' ');
return;
}
root=preOrder[preStart];
for(i=inStart;i<=inEnd;i++)
if(inOrder[i]==root)
break;
leftsize=i-inStart;
rightsize=inEnd-i; nextLeftpreStart=preStart+1;
nextLeftpreEnd=preStart+leftsize;
nextRightpreStart=nextLeftpreEnd+1;
nextRightpreEnd=nextLeftpreEnd+rightsize; nextLeftinStart=i-leftsize;
nextLeftinEnd=i-1;
nextRightinStart=i+1;
nextRightinEnd=i+rightsize; if(i!=inStart){
Process(preOrder,inOrder,nextLeftpreStart,nextLeftpreEnd,nextLeftinStart,nextLeftinEnd);
} if(i!=inEnd){
Process(preOrder,inOrder,nextRightpreStart,nextRightpreEnd,nextRightinStart,nextRightinEnd);
}
printf("%d",root);
printcount++;
if (printcount!=length)
putchar(' ');
return;
} void Input(int pre[],int in[])
{
int i,n,num,idxforpush=0,idxforpop=0;
char* s;
char temp[50];
ptrStack workstack=CreateStack(); scanf("%d",&n);
length=n;
n=n*2;
for(i=0;i<n;i++)
{ scanf("%s",temp);
s=strchr(temp,'h');
if(s!=NULL){
scanf("%d",&num);
pre[idxforpush]=num;
Push(workstack,num);
// printf("----pre----%d\n",pre[idxforpush]);//for test
idxforpush++; }
else{
in[idxforpop]=Pop(workstack);
// printf("++++ in++++%d\n",in[idxforpop]);//for test
idxforpop++;
} }
DestroyStack(workstack);
}
int main()
{
int i;
int in[MAXLEN];
int pre[MAXLEN];
Input(pre,in);
// for(i=0;i<length;i++) printf("idx %d in %d pre %d\n",i,in[i],pre[i]);//for test;
Process(pre,in,0,length-1,0,length-1);
}

  

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

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

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

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

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

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

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

  4. A1020 Tree Traversals (25 分)

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

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

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

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

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

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

    An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example ...

  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. redirect与forward的区别

    文件名称:forward.jsp <html> <head><title> 跳转 </title> </head> <body> ...

  2. 在虚拟机里安装windows或Linux系统时,安装窗口过大按钮有时点不到解决办法(图文详解)

    不多说,直接上干货! 问题详情 解决办法 很简单快捷的解决办法,就是快捷键ALT+F7,可以拖动窗口的位置. 成功!

  3. AJPFX总结方法里的属性

    嵌套循环:循环里套循环 假设外循环的循环次数是m次,内循环的循环次数是n次,那么内层循环的循环次数需要 m * n次.   Eg:利用for循环语句的嵌套打印出乘法口诀表   class break1 ...

  4. Haproxy+Rabbitmq中的问题

    问题一.Rabbitmq集群搭建完成 某个集群节宕机后 无法添加失败 解决办法:停掉所有Rabbitmq服务 并删除集群文件C\Users\Administrator\AppData\Roaming\ ...

  5. c/s架构搭建

    1.socket(套接字) Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Socket接 ...

  6. How the performance impacts your revenue-性能影响营收

    看完国外一个APM厂商最后的一个业务介绍视频,终于想通了PE领域中最顶层的应用目标,也就是如标题所云.那么这个影响效果是如何做到的?最终的步骤其实很简单,也就是利用大数据进行分析.而自己先前还没有想到 ...

  7. 洛谷 P1454 圣诞夜的极光 == codevs 1293 送给圣诞夜的极光

    题目背景 圣诞夜系列~~ 题目描述 圣诞老人回到了北极圣诞区,已经快到12点了.也就是说极光表演要开始了.这里的极光不是极地特有的自然极光景象.而是圣诞老人主持的人造极光. 轰隆隆……烟花响起(来自中 ...

  8. HDU 4281 (状态压缩+背包+MTSP)

    Judges' response Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. springcloud 之 bus 消息总线

    在分布式系统中,我们通常使用轻量级消息代理(rabbitmq.kafuka)建立一个公共的主题,让所有的微服务都链接进来,并且监听消费这个主题的内容,我们就称这个主题是 消息总线. (可以用作配置文件 ...

  10. html归纳

      onload的用法 表格属性 定时器(测试能否让for循环暂停5秒) 实现表格的滚动条效果 ① table中th的样式:  white-space: nowrap;  单元格内容不换行:② 设置装 ...