题目地址 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. Backbone.js入门教程第二版笔记(1)

    1.模块 集合 视图 和事件的一个综合例子 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q ...

  2. VC++编译出错:LNK1123: 转换到 COFF 期间失败: 文件无效或损坏

    解决方法: 1.搜索C盘下的cvtres.exe,结果得到类似这样的列表: C:\Program Files\Microsoft Visual Studio 10.0\VC\bin C:\Window ...

  3. springdata-jpa 八种查询方法

    使用:maven+Spring+jpa+Junit4 查询方式:SQL,JPQL查询,Specification多条件复杂查询 返回类型:list<POJO>,list<Stinrg ...

  4. 【HEVC简介】High Level Syntax

    参考文献:见<High Efficiency Video Coding (HEVC)>High Level Syntax章节 <HEVC标准介绍.HEVC帧间预测论文笔记>系列 ...

  5. 洛谷 P1163 银行贷款

    题目描述 当一个人从银行贷款后,在一段时间内他(她)将不得不每月偿还固定的分期付款.这个问题要求计算出贷款者向银行支付的利率.假设利率按月累计. 输入输出格式 输入格式: 输入文件仅一行包含三个用空格 ...

  6. swift Equatable 的缺省实现

    Starting from Swift 4.1, all you have to is to conform to the Equatable protocol without the need of ...

  7. Python3基础教程(十九)—— 项目结构

    本节阐述了一个完整的 Python 项目结构,你可以使用什么样的目录布局以及怎样发布软件到网络上. 创建Python项目 我们的实验项目名为 factorial,放到 /home/shiyanlou/ ...

  8. docker 应用数据的管理

    容器数据存储的三种方式 docker volume docker管理素质及文件系统的一部分,保存数据最佳方式 bind mounts   将宿主机的文件映射到容器里 tmpfs   存储在宿主机的内存 ...

  9. CPP-STL:vector容器

    1.     vector容器简介: vector向量容器是一种随机访问的数组类型,它提供了对数组元素的快速访问.随机访问,以及在序列尾部快速.随机地插入和删除操作.它类似于数据结构中的队列.数组和堆 ...

  10. ansible2.7学习笔记系列

    写在前面:ansible的资料网上很多,本人也是参考网上资料,做总结,如有错误,麻烦指出,谢谢. 所谓学习笔记,就是不断成长的过程,也许一段时间后有更深入理解了,就会继续更新笔记. 笔记定位:目前写的 ...