题目:

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. 云计算之路-试用Azure:如何建立虚拟机之间的内网连接

    在阿里云上,同一个帐户创建的所有虚拟机(云服务器)之间的内网是直接连通的.而Azure则完全不一样,一开始使用时有点不知所措,后来摸索出来了——在Azure中只有处于同一个虚拟网络(Virtual N ...

  2. Unity3D优化之合并网格

    原文地址点击这里

  3. error: could not install *smartsocket* listener: cannot bind to 127.0.0.1:5037

    问题原因:端口5037被占用 解决方案: 方式一:可以用cmd命令 C:\Users\Administrator>netstat -ano | findstr "5037" ...

  4. 伪分布模式安装hadoop

    准备工具: 虚拟机:VMware Linux系统:CentOS hadoop-1.1.2.tar.gz jdk-7u75-linux-x64.gz CentOS的网络配置 1.设置主机中VMware ...

  5. [Exception Android 20] - Error:Execution failed for task ':app:processDebugResources'

    Error:Execution failed for task ':app:processDebugResources'. > com.android.ide.common.process.Pr ...

  6. Linux-Nginx-关闭进程

    当然就仅仅是介绍一条命令了,就这么简单. nginx默认创建一个工作进程 root 2713 1 0 07:56 ? 00:00:00 nginx: master process ../sbin/ng ...

  7. 【小程序】微信小程序开发—弹出框

    1. <span style="font-family:Comic Sans MS;font-size:18px;color:#333333;"><view cl ...

  8. Codeigniter MongoDB扩展之使用Aggregate实现Sum方法

    本篇文章由:http://xinpure.com/codeigniter-mongodb-extension-using-aggregate-sum-method/ Codeigniter Mongo ...

  9. phpexcel表的一些设置

    $objPHPExcel = new PHPExcel(); $objPHPExcel->setActiveSheetIndex(0); //set default styles$objPHPE ...

  10. android 基于分包方案的修复

    # 本demo实现原理来自 https://github.com/dodola/HotFix https://zhuanlan.zhihu.com/p/20308548 # Anti类功能,及其原理 ...