题目地址 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. 494 Target Sum 目标和

    给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S.现在你有两个符号 + 和 -.对于数组中的任意一个整数,你都可以从 + 或 -中选择一个符号添加在前面.返回可以使最终数组和为 ...

  2. PHP连接数据操作步骤

    数据库的操作步骤: 端口号:0到65535 3306:mysql数据库的默认端口号(可修改) mysql_connect(“本机地址”,“用户名”,“密码”,); new_link:如果用同样的参数第 ...

  3. Jenkins视图使用--添加删除视图

    job建立的特别多的时候,我们可能不太容易找到自己的某个job,这时,我们就可以在Jenkins中建立视图.job的视图类似于我们电脑上的文件夹.可以通过一些过滤规则,将已经建好的job过滤到视图中, ...

  4. 命令模式和php实现

    命令模式: 命令模式(Command Pattern):将一个请求封装为一个对象,从而使我们可用不同的请求对客户进行参数化:对请求排队或者记录请求日志,以及支持可撤销的操作.命令模式是一种对象行为型模 ...

  5. Maximal Discount

    Description: Linda is a shopaholic. Whenever there is a discount of the kind where you can buy three ...

  6. flutter基础

    1.flutter安装 1.参考官网安装sdk https://flutter.io/get-started/install 安卓和IOS需要分别配置对应的开发环境,安卓建议使用as开发,安装Flut ...

  7. SQLyog连接MySQL时出现的2058错误解决方法

    配置新连接报错:错误号码 2058,分析是 mysql 密码加密方法变了. 解决方法:windows 下cmd 登录 mysql -u root -p 登录你的 mysql 数据库,然后执行这条SQL ...

  8. SSAS 系列01- DAX公式常用公式

    计算第一次购买时间 CALCULATE(FIRSTDATE(FactInternetSales[OrderDate]),ALLEXCEPT(FactInternetSales,FactInternet ...

  9. (一)VMware Harbor 简介

    (一)Harbor简介 Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器,通过添加一些企业必需的功能特性,例如安全.标识和管理等,扩展了开源Docker Distribu ...

  10. 指针-AC自动机

    大家都不喜欢指针,但是这个AC自动机仿佛不用不行…… 先引用我最喜欢的话:“AC自动机,不是自动AC的机器.” 如果写不好还可能一直WA AC自动机是KMP与Trie树的完美结合,适用于多字符串匹配, ...