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 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分)的更多相关文章
- 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-树3 Tree Traversals Again (25 分)
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example ...
- 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 ...
随机推荐
- 正则表达式test报错 is not a function
var reg = "/^1[34578]\d{9}$/"; //错误格式,这是一个字符串 var reg2 = /^1[34578]\d{9}$/; //正确格式 reg .te ...
- hadoop-0.20.2伪分布式安装简记
1.准备环境 虚拟机(redhat enterprise linux 6.5) jdk-8u92-linux-x64.tar.gz hadoop-0.20.2.tar.gz 2.关闭虚拟机的防火墙,s ...
- 浅析cookie
基本概念:cookie是指web浏览器存储的少量数据,该数据会在每次请求一个相关的URL时自动传到服务器中. 以博客园为例,我们看看cookie有哪些属性: 1.Name:cookie的名称: 2. ...
- MySQL系列:隐式类型转化可能带来的坑
在开发规范中,我们往往会要求研发避免在where条件中出现隐式类型转换,这么要求大概有以下两方面的原因: 隐式类型转换可能导致索引失效: 隐式类型转换可能产生非预期的结果. 注:这里说的是隐式类型转换 ...
- Android设计模式——MVP
一.什么是MVP MVP:全称 Model-View-Presenter. MVP框架由3部分组成:View层负责显示,Presenter层负责逻辑处理,Model层提供数据. View:负责绘制UI ...
- 初次使用引用外部js心得
在外部引用自己编辑的js时建立链接写在头部中是会出错的,如下图 错误如下: 这是一个是我初学时遇到的一个算是低级错误吧,看到这个错误,我以为的是我引用的js中编辑的代码是不是哪里写错了,但是看了好多遍 ...
- 《Python基础教程》 读书笔记 第六章 抽象 函数 参数
6.1创建函数 函数是可以调用(可能包含参数,也就是放在圆括号中的值),它执行某种行为并且返回一个值.一般来说,内建的callable函数可以用来判断函数是否可调用: >>> x=1 ...
- TFS2010升级至TFS2013完全指南(更换服务器)
一.背景: 公司已使用tfs2010很长时间,目前随着公司的发展,项目越来越少,而产品越来越多,采用的开发模式,也逐渐从瀑布式.迭代式转向敏捷开发.为了更好的支持产品研发,决定将tfs ...
- 洛谷 P1030 求先序排列
题目描述 给出一棵二叉树的中序与后序排列.求出它的先序排列.(约定树结点用不同的大写字母表示,长度<=8). 输入输出格式 输入格式: 2行,均为大写字母组成的字符串,表示一棵二叉树的中序与后序 ...
- 如何安装多个pip包
要在命令行上安装多个软件包,只需将它们作为以空格分隔的列表传递,例如: pip install wsgiref boto