题目地址 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. 利用lsof去查看Unix/Linux进程打开了哪些文件

    利用lsof去查看Unix/Linux进程打开了哪些文件 今天用了一下lsof,发现这个linux的小工具,功能非常强大而且好用. 我们可以方便的用它查看应用程序进程打开了哪些文件或者对于特定的一个文 ...

  2. 127 Word Ladder 单词接龙

    给出两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列,转换需遵循如下规则:    每次只能改变一个字母.    变换过程中的 ...

  3. AndroidStudio碰到的各种问题

    源码已经下载了,但是为毛关联不了? 我的源码默认是下载在Sdk\sources\android-23\目录下面的,以前开发的时候都是自动关联的,今天碰到了怎么刷新,怎么关联都不行. 解决方式为: 1. ...

  4. canvas基础绘制-绚丽倒计时

    效果图: html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  5. dzzoffice网盘应用有着最强大的团队、企业私有网盘功能,并且全开源无功能限制。

    企业,团队多人使用dzzoffice的网盘应用,灵活并且功能强大. 支持个人网盘,机构部门,群组,并可根据使用情况开启关闭.例如可只开启群组功能.   可通过后缀,标签自定义类型进行快捷筛选   全面 ...

  6. MMRecovery下载和恢复方法教程

    如何能下载到最新版本的恢复软件MMRecovery?下载后我们如何能够快速的使用起来呢?好的,我们接下来就开始这些内容. 一.下载最新版本的恢复软件MMRecovery 1.官方网站下载,如下图在浏览 ...

  7. Android(java)学习笔记181:多媒体之图片画画板案例

    1.首先我们编写布局文件activity_main.xml如下: <RelativeLayout xmlns:android="http://schemas.android.com/a ...

  8. Django-常用设置(settings.py)

    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 当前项目的根目录,Django会依此来定位工程内的相关文件 ...

  9. jquery命名冲突

    nodeName是jquery的关键字

  10. 7-Java-C(小题答案)

    1:58497 2:171700 3:145 4:i + j+2 == k+1 || i + k+2 == j+1 || k + j+2 == i+1 5:s + " " + (c ...