题目:

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. [Android Pro] Notification的使用

    Android 4.0以前: 1: 普通的notification private static final int NOTIFY_ID = 0; notificationManager = (Not ...

  2. 更改Mysql数据库存储位置

    默认安装位置 C:\Program Files\MySQL\MySQL Server 5.7 一.首先把mysql的服务先停掉. 二.更改MySQL配置文件My.ini中的数据库存储主路径 打开MyS ...

  3. java实现快速排序算法

    1.算法概念. 快速排序(Quicksort)是对冒泡排序的一种改进.由C. A. R. Hoare在1962年提出.2.算法思想. 通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据 ...

  4. 云计算之路-试用Azure:数据库备份压缩文件在虚拟机上的恢复速度测试

    测试环境:Windows Azure上海机房,虚拟机配置为大型(四核,7 GB 内存),磁盘情况见下图. 数据库备份压缩文件大于为12.0 GB (12,914,327,552 bytes),放置于T ...

  5. vue - webpack.dev.conf.js for merge

    webpack-merge提供了一个merge连接数组并合并创建新对象的对象的函数.如果遇到函数,它将执行它们,通过算法运行结果,然后再次将返回的值包装在函数中. 这种行为在配置webpack时特别有 ...

  6. python 字典的KeyError处理方法

    先看一段代码: user = dict(name="brainliao", age=32) print(user["sex"]) 运行结果如下: user这个字 ...

  7. 算法笔记_163:算法提高 最大乘积(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 对于n个数,从中取出m个数,如何取使得这m个数的乘积最大呢? 输入格式 第一行一个数表示数据组数 每组输入数据共2行: 第1行给出总共的数 ...

  8. gsub

    gsub("([ab])", "\\1_\\1_", "abc and ABC")[1] "a_a_b_b_c a_a_nd AB ...

  9. php json_decode失败,返回null

    在使用json_decode之前,一定得保证字符串是utf-8编码,而执行json_decode失败的原因有很多,罗列如下: 1)编码不对: 2)字符串格式不对: 3)字符串格式对,但是有异常字符: ...

  10. std::vector<char> 转 const char

    std::stringstream oss; for(unsigned int i=0;i < buffer->size();i++){ oss<<(*buffer)[i];  ...