这题是第二次做了,两次都不是独立完成,不过我发现我第一次参考的程序,也是参考老师(陈越)的范例做出来的。我对老师给的做了小幅修改,因为我不想有全局变量的的存在,所以我多传了三个参数进去。正序遍历每次都是从1到N吗?看题目我认为应该是,结果我错了,我是对比正确的程序一点点修改才发现的,不容易啊。下面是题目及程序

 #include <stdio.h>
#include <stdlib.h>
#include <string.h> typedef struct
{
int * a;
int top;
}SeqStack; void push(SeqStack * pS, int X);
int pop(SeqStack * pS);
void solve(int * pre, int * in, int * post, int preL, int inL, int postL, int n); int main()
{
// freopen("in.txt", "r", stdin); // for test
int i, N;
scanf("%d", &N); SeqStack S;
S.a = (int *)malloc(N * sizeof(int));
S.top = -;
int pre[N], in[N], post[N]; char chars[];
char * str = chars;
int X, pre_index, in_index;
pre_index = in_index = ;
for(i = ; i < * N; i++)
{
scanf("%s", str);
if(strcmp(str, "Push") == )
{
scanf("%d", &X);
pre[pre_index++] = X;
push(&S, X);
}
else
in[in_index++] = pop(&S);
} solve(pre, in, post, , , , N);
for(i = ; i < N; i++)
{
printf("%d", post[i]);
if(i < N - )
printf(" ");
else
printf("\n");
}
// fclose(stdin); // for test
return ;
} void push(SeqStack * pS, int X)
{
pS->a[++(pS->top)] = X;
} int pop(SeqStack * pS)
{
return pS->a[pS->top--];
} void solve(int * pre, int * in, int * post, int preL, int inL, int postL, int n)
{
int i, root, L, R; if(n == )
return;
if(n == )
{
post[postL] = pre[preL];
return;
}
root = pre[preL];
post[postL + n - ] = root;
for(i = ; i < n; i++)
if(in[inL + i] == root)
break;
L = i;
R = n - L - ;
solve(pre, in, post, preL + , inL, postL, L);
solve(pre, in, post, preL + L + , inL + L + , postL + L, R);
}

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 N (<=30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines 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

03-树2 Tree Traversals Again的更多相关文章

  1. PAT-1086(Tree Traversals Again)Java语言实现+根据中序和前序遍历构建树并且给出后序遍历序列

    Tree Traversals Again Tree Traversals Again 这里的第一个tip就是注意到非递归中序遍历的过程中,进栈的顺序恰好是前序遍历的顺序,而出栈的顺序恰好是中序遍历的 ...

  2. HDU 1710 Binary Tree Traversals(树的建立,前序中序后序)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  3. Tree Traversals

    Tree Traversals 原题链接 常见的二叉树遍历的题目,根据后序遍历和中序遍历求层次遍历. 通过后序遍历和中序遍历建立起一棵二叉树,然后层序遍历一下,主要难点在于树的建立,通过中序遍历和后序 ...

  4. 树-伸展树(Splay Tree)

    伸展树概念 伸展树(Splay Tree)是一种二叉排序树,它能在O(log n)内完成插入.查找和删除操作.它由Daniel Sleator和Robert Tarjan创造. (01) 伸展树属于二 ...

  5. HDU1710Binary Tree Traversals

    HDU1710Binary Tree Traversals 题目大意:给一个树的前序遍历和中序遍历,要求输出后序遍历. (半年前做这道题做了两天没看懂,今天学了二叉树,回来AC了^ ^) 首先介绍一下 ...

  6. 03-树2. Tree Traversals Again (25)

    03-树2. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...

  7. PAT1086:Tree Traversals Again

    1086. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  8. Binary Tree Traversals(HDU1710)二叉树的简单应用

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  9. PAT 1020 Tree Traversals[二叉树遍历]

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  10. PAT 1086 Tree Traversals Again

    PAT 1086 Tree Traversals Again 题目: An inorder binary tree traversal can be implemented in a non-recu ...

随机推荐

  1. 再说TCP神奇的40ms

    版权声明:本文由安斌原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/186 来源:腾云阁 https://www.qclou ...

  2. maven 添加支持编译jdk1.7

     1.在<profiles>元素内增加如下内容   <profile>     <id>jdk17</id>     <activation> ...

  3. 实现 像网易云音乐 播放列表那样的弹出型Dialog

    如图 所示是点击Test之后的 弹出的Dialog (请无视我工程的命名) 20161017/**加入点击回调,假设dialog里放了一个TextView*/ 得先写一个点击回调 public int ...

  4. 在ubuntu下真机调试android程序出现设备没有访问权限

    今天把android的开发环境从windows平台切换到了ubuntu上. java jdk android-adt android-ndk都下好,环境变量都配好之后, 在调试程序的时候,出现设备没有 ...

  5. Mongoose学习参考文档

    一.快速通道 1.1 名词解释 Schema : 一种以文件形式存储的数据库模型骨架,不具备数据库的操作能力 Model : 由Schema发布生成的模型,具有抽象属性和行为的数据库操作对 Entit ...

  6. 也谈谈 Redis 和 Memcached 的区别

    本文作者: 伯乐在线 - 朱小厮 . 说到redis就会联想到memcached,反之亦然.了解过两者的同学有那么个大致的印象: redis与memcached相比,比仅支持简单的key-value数 ...

  7. EasyUI DataGrid View

    http://www.jeasyui.com/easyui/datagrid-detailview.js 前提一定要引入:datagrid-detailview.js主要是三个属性和普通的datgag ...

  8. 必须关注的25位知名JavaScript开发者

    必须关注的25位知名JavaScript开发者 发表于2012-08-07 17:30| 16215次阅读| 来源Crossrider Blog| 46 条评论| 作者Crossrider Blog ...

  9. LOOPS(HDU 3853)

    LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)Total Sub ...

  10. spring校验相关

    转载:http://elim.iteye.com/blog/1812584 对于任何一个应用而言在客户端做的数据有效性验证都不是安全有效的,这时候就要求我们在开发的时候在服务端也对数据的有效性进行验证 ...