03-树3. Tree Traversals Again (25)将先序遍历和中序遍历转为后序遍历
03-树3. Tree Traversals Again (25)
题目来源:http://www.patest.cn/contests/mooc-ds/03-%E6%A0%913
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 题目实质是通过先序遍历和中序遍历建树,再后序遍历树。
解题思路
1. 通过输入建树
Push操作代表新建一个节点,将其与父节点连接并同时压栈
Pop操作,从栈顶弹出一个节点
2. 后序遍历:递归实现
代码如下:
#include <cstdio>
#include <cstring>
#include <cstdlib> #define STR_LEN 5
#define MAX_SIZE 30 typedef struct Node
{
int data;
struct Node *left, *right;
}* treeNode; treeNode Stack[MAX_SIZE];
int values[MAX_SIZE]; int num = ;
int top = -; void Push(treeNode tn);
treeNode Pop();
treeNode Top();
bool isEmpty(); void PostOrderTraversal(treeNode root); int main()
{
int n;
char operation[STR_LEN];
treeNode father, root;
bool findRoot = , Poped = ; scanf("%d", &n);
for (int i = ; i < * n; i++)
{
scanf("%s", operation);
if (strcmp(operation, "Push") == )
{
int value;
scanf("%d", &value);
treeNode newNode;
newNode = (treeNode)malloc(sizeof(struct Node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
if (!findRoot)
{
root = newNode; //根节点
Push(newNode);
findRoot = ;
}
else
{
if (!Poped) //如果前一个操作不是pop,则父节点为栈顶元素
father = Top();
if (father->left == NULL)
father->left = newNode;
else
father->right = newNode;
//printf("%d\n", newNode->data);
Push(newNode);
}
Poped = ;
}
else
{
father = Pop();
Poped = ;
}
}
PostOrderTraversal(root); for (int i = ; i < num-; i++)
printf("%d ", values[i]);
printf("%d\n", values[num-]); return ;
} void PostOrderTraversal(treeNode root)
{
treeNode tn = root;
if(tn)
{
PostOrderTraversal(tn->left);
PostOrderTraversal(tn->right);
values[num++] = tn->data; //将后序遍历出的节点值存入数组便于格式化打印
}
} void Push(treeNode tn)
{
Stack[++top] = tn;
} treeNode Pop()
{
return Stack[top--];
} bool isEmpty()
{
return top == -;
} treeNode Top()
{
return Stack[top];
}
03-树3. Tree Traversals Again (25)将先序遍历和中序遍历转为后序遍历的更多相关文章
- 03-树2. Tree Traversals Again (25)
03-树2. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...
- pat03-树3. Tree Traversals Again (25)
03-树3. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...
- pat1086. Tree Traversals Again (25)
1086. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- PTA 03-树3 Tree Traversals Again (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/667 5-5 Tree Traversals Again (25分) An inor ...
- PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习
1086 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-recu ...
- 数据结构课后练习题(练习三)7-5 Tree Traversals Again (25 分)
7-5 Tree Traversals Again (25 分) An inorder binary tree traversal can be implemented in a non-recu ...
- PAT 1043 Is It a Binary Search Tree (25分) 由前序遍历得到二叉搜索树的后序遍历
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- PAT Advanced 1086 Tree Traversals Again (25) [树的遍历]
题目 An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For exam ...
- 1086. Tree Traversals Again (25)
题目如下: An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For e ...
随机推荐
- Nginx高性能优化
#Nginx配置文件优化 worker_processes ; # nginx进程数,建议按照cpu数目来指定,一般为它的倍数. worker_cpu_affinity ; # 为每个进程分配CPU的 ...
- PHPCMS的产品筛选功能
如下图所示功能: 首先,用下面这些代码替换掉phpcms/libs/functions/extention.func.php的内容 <?php /** * extention.func.php ...
- golang笔记2_程序结构
golang程序结构 2.1 命名 Golang中的命名遵循这样一个简单原则,名字的开头必须是字母或者下划线,后面跟字母.数字或者下划线(这里与C语言中是一致的). 在函数内部声明的实体,即局部变量, ...
- Qt 编程指南
Qt 编程指南 持续关注一本正在编写的Qt编程指南,期待作者早日完成创作.
- .NET 4.0 Tuple 元组
Tuple是.NET 4.0的新特性,主要功能是动态返回数据结构,也可以用做临时数据结构. 原来做一些功能时需要一个方法返回几个值,有两种方法: 1. 非常难看.难用的OUT参数: 2. 新写一个实体 ...
- 第9次Scrum会议(10/21)【欢迎来怼】
一.小组信息 队名:欢迎来怼小组成员队长:田继平成员:李圆圆,葛美义,王伟东,姜珊,邵朔,冉华小组照片 二.开会信息 时间:2017/10/21 17:20~17:45,总计25min.地点:东北师范 ...
- 博弈--ZOJ 3084 S-Nim(SG)
题意: 首先输入K 表示一个集合的大小 之后输入集合 表示对于这对石子只能去这个集合中的元素的个数 之后输入 一个m 表示接下来对于这个集合要进行m次询问 之后m行 每行输入一个n 表示有n个堆 ...
- HDU 4055 Number String dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4055 Number String Time Limit: 10000/5000 MS (Java/O ...
- (六)Jmeter重要组件的执行顺序及作用域
一.Jmeter重要组件: 1)配置元件---Config Element: 用于初始化默认值和变量,以便后续采样器使用.配置元件大其作用域的初始阶段处理,配置元件仅对其所在的测试树分支有效,如,在同 ...
- linux 转移mysql文件操作流程
1.现将mysql停服 2.将文件拷贝到指定目录cp ./sales_trade_2.ibd /db/data/mysql/data_warehouse/sales_trade_2.ibd 3.检查新 ...