03-树2 Tree Traversals Again
这题是第二次做了,两次都不是独立完成,不过我发现我第一次参考的程序,也是参考老师(陈越)的范例做出来的。我对老师给的做了小幅修改,因为我不想有全局变量的的存在,所以我多传了三个参数进去。正序遍历每次都是从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的更多相关文章
- PAT-1086(Tree Traversals Again)Java语言实现+根据中序和前序遍历构建树并且给出后序遍历序列
Tree Traversals Again Tree Traversals Again 这里的第一个tip就是注意到非递归中序遍历的过程中,进栈的顺序恰好是前序遍历的顺序,而出栈的顺序恰好是中序遍历的 ...
- HDU 1710 Binary Tree Traversals(树的建立,前序中序后序)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- Tree Traversals
Tree Traversals 原题链接 常见的二叉树遍历的题目,根据后序遍历和中序遍历求层次遍历. 通过后序遍历和中序遍历建立起一棵二叉树,然后层序遍历一下,主要难点在于树的建立,通过中序遍历和后序 ...
- 树-伸展树(Splay Tree)
伸展树概念 伸展树(Splay Tree)是一种二叉排序树,它能在O(log n)内完成插入.查找和删除操作.它由Daniel Sleator和Robert Tarjan创造. (01) 伸展树属于二 ...
- HDU1710Binary Tree Traversals
HDU1710Binary Tree Traversals 题目大意:给一个树的前序遍历和中序遍历,要求输出后序遍历. (半年前做这道题做了两天没看懂,今天学了二叉树,回来AC了^ ^) 首先介绍一下 ...
- 03-树2. Tree Traversals Again (25)
03-树2. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT1086:Tree Traversals Again
1086. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- Binary Tree Traversals(HDU1710)二叉树的简单应用
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- PAT 1020 Tree Traversals[二叉树遍历]
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT 1086 Tree Traversals Again
PAT 1086 Tree Traversals Again 题目: An inorder binary tree traversal can be implemented in a non-recu ...
随机推荐
- centos7 使用 omnibus包安装方式,安装 gitlab7.4
centos7 使用 omnibus包安装方式,安装 gitlab7.4 1: gitlab是一个开源的软件,类似于github.com那样的git代码管理仓库: 官网 https://about.g ...
- VC++导入导出类
一.导出类 VC++中导出类很简单,下面列出了两个等价的方法: 方法1: class __declspec(dllexport) CTest { public: int m_nValue ...
- mouseenter和mouseout中间的时间控制
为了防止鼠标快速滑过div而加的时间限制: 在看延迟绑定时候看到,这也算是延迟绑定了?:(20130909) <!DOCTYPE html> <html lang="en& ...
- 集成 Apple Pay
作者感言 在中秋过后终于把国内的三大支付平台SDK集成都搞定了, 现在我们终于可以来研究Apple自家的支付Apple Pay最后:如果你有更好的建议或者对这篇文章有不满的地方, 请联系我, 我会参考 ...
- js从千分位格式
从千分位格式化谈JS性能优化 http://heeroluo.net/article/detail/115 方法六 // 方法六 function toThousands(num) { ).toStr ...
- 使用Astah繪製UML圖形(转)
http://www.dotblogs.com.tw/clark/archive/2015/02/12/149483.aspx
- attachEvent ,addEventListener
if (window.attachEvent) { window.attachEvent("onload", remove); ...
- 你所知道的Java单例模式并不是单例模式
当我们搜索单例模式的时候,能看到很多例子,什么懒汉式.饿汉式,大概如下: public class Singleton { private static Singleton instance=null ...
- Assert断言测试
assert编写代码时,我们总是会做出一些假设,断言就是用于在代码中捕捉这些假设,可以将断言看作是异常处理的一种高级形式.断言表示为一些布尔表达式,程序员相信在程序中的某个特定点该表达式值为真.可以在 ...
- android应用程序如何调用支付宝接口(转)
最近在做一个关于购物商城的项目,项目里面付款这块我选的是调用支付宝的接口,因为用的人比较多. 在网上搜索了以下,有很多这方面的教程,但大部分教程过于陈旧,而且描述的过于简单.而且支付宝提供的接口一直在 ...