题目:

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

分析: 主要是根据输入创建一个二叉树,然后进行后续遍历

代码:

#pragma mark -Tree Traversals Again
#include <stdio.h> typedef struct traversalTreeNode {
int value;
struct traversalTreeNode *left;
struct traversalTreeNode *right;
} TraversalTreeNode; int flag; TraversalTreeNode *createTraversalTreeNode(int value)
{
TraversalTreeNode *node = (TraversalTreeNode *)malloc(sizeof(TraversalTreeNode));
node->value = value;
node->left = NULL;
node->right = NULL;
return node;
} void postorderTraversal(TraversalTreeNode *head)
{
if (head) {
postorderTraversal(head->left);
postorderTraversal(head->right); if (flag == ) {
printf("%d", head->value);
flag = ;
} else {
printf(" %d", head->value);
}
}
} int main()
{
int nodeNum = ;
scanf("%d", &nodeNum); int operationCount = * nodeNum;
TraversalTreeNode *a[]; int top = -;
// 第一个节点肯定是PUSH
int index = -;
scanf("%*s %d", &index);
TraversalTreeNode *head = createTraversalTreeNode(index);
a[] = head;
top = ; TraversalTreeNode *popItem = NULL;
for (int i = ; i < operationCount; i++) {
int index = -;
char str[];
scanf("%s", str);
unsigned long len = strlen(str);
if (len >= ) {
scanf("%d", &index);
TraversalTreeNode *newNode = createTraversalTreeNode(index);
if (popItem) {
if (!popItem->left) {
popItem->left = newNode;
} else {
popItem->right = newNode;
}
} else {
if (!a[top]->left) {
a[top]->left = newNode;
} else {
a[top]->right = newNode;
}
} top++;
a[top] = newNode;
popItem = NULL;
} else {
popItem = a[top];
a[top] = NULL;
top--;
}
} flag = ;
postorderTraversal(head);
}

运行结果:

PAT006 Tree Traversals Again的更多相关文章

  1. Tree Traversals

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

  2. HDU 1710 二叉树的遍历 Binary Tree Traversals

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

  3. hdu1710(Binary Tree Traversals)(二叉树遍历)

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

  4. HDU1710Binary Tree Traversals

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

  5. HDU-1701 Binary Tree Traversals

    http://acm.hdu.edu.cn/showproblem.php?pid=1710 已知先序和中序遍历,求后序遍历二叉树. 思路:先递归建树的过程,后后序遍历. Binary Tree Tr ...

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

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

  7. HDU 1710-Binary Tree Traversals(二进制重建)

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

  8. PAT1086:Tree Traversals Again

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

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

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

随机推荐

  1. Linux下libsvm的安装及简单练习

    引文:常常在看paper的时候.就看到svm算法,可是要自己来写真的是难于上青天呀! 所幸有一个libsvm的集成软件包给我们使用,这真的是太好了.以下简介下怎么来使用它吧! LIBSVM是一个集成软 ...

  2. Linux系统目录结构,Shell脚本;关闭和开启防火墙

    Linux系统目录结构 目录 描述 备注 /bin a.存放着最经常使用的命令 b.可执行文件,用户命令 c.构建最小系统所需要的命令 /boot a.内核与启动文件 b.系统启动相关文件 c.启动L ...

  3. 使用xml-rpc调试openerp模块中的函数

    运行openerp模块中的函数 有很多方式, 可以在视图中加个按钮然后点击它, 也可以在集成开发环境中强制执行它. 不过, 用python写个小脚本,xml-rpc调用直接执行它, 无疑是最简便的方法 ...

  4. An error occured while handling a json request

    修复方法: sudo pip install werkzeug==0.8.3

  5. 关于Future.cancel(mayInterruptIfRunning)方法的参数的问题

    mayInterruptIfRunning设成false话,不允许在线程运行时中断,设成true的话就允许. 可以参考下面的代码来理解,如果设为false的话,会打印到99999,如果设成true的话 ...

  6. shell脚本中执行mysql 语句,去除warning using a password on the command line interface can be insecure信息

    方法二:使用mysql参数的方法 mysql -u$user -p$pass -D $db -e "select host from user;"当然,可以通过将传参的方式来传递 ...

  7. 解决chrome和firefox flash不透明的方法

    透明flash在IE内核的浏览器下正常.在chrome和火狐下不透明了. 解决方法: <object height="377" width="712" c ...

  8. FreeMarker静态化文件解决SEO推广问题

    1.问题背景 SEO一直是站点对外推广的一个重要手段,如何可以让搜索引擎高速搜索到站点对于增强站点的浏量,提升站点对外形象有着重要意义.那么如何可以对SEO进行优化呢?一个很经常使用的手段就是在网页的 ...

  9. Editplus 文件中批量搜索字符串的技巧

    常规情况下,我们利用Crtl+F可以在文档中查找字符串,进行替换等操作. 但要有的时候,我们要在大量文件中做这种查找操作,显然,一个个的打开文档是不现实的. 比如: 最近,谷歌被墙的很厉害,导致很多w ...

  10. .Net并行编程高级教程(二)-- 任务并行

    前面一篇提到例子都是数据并行,但这并不是并行化的唯一形式,在.Net4之前,必须要创建多个线程或者线程池来利用多核技术.现在只需要使用新的Task实例就可以通过更简单的代码解决命令式任务并行问题. 1 ...