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 ...
随机推荐
- 最短路径算法(I)
弗洛伊德算法(Floyed-Warshall) 适用范围及时间复杂度 该算法的时间复杂度为O(N^3),适用于出现负边权的情况. 可以求取最短路径或判断路径是否连通.可用于求最小环,比较两点之间的大小 ...
- django项目中关于跨域CORS
1.使用django-cors-headers扩展,但首先进行安装 2.在配置中添加应用 3.在中间层中设置:“corsheaders.middleware.CorsMiddleware” 4.添加C ...
- 大前端-全栈-node+easyui+express+vue+es6+webpack+react
作者声明:本博客中所写的文章,都是博主自学过程的笔记,参考了很多的学习资料,学习资料和笔记会注明出处,所有的内容都以交流学习为主.有不正确的地方,欢迎批评指正 视频来源:https://www.bil ...
- Scrum立会报告+燃尽图(十一月十八日总第二十六次):功能开发与讨论贡献分配规则
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284 项目地址:https://git.coding.net/zhang ...
- 福大软工1816:Beta(7/7)
Beta 冲刺 (7/7) 队名:第三视角 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务 文字/口头描述 组织会议 wxpy中多个功能的开发 整 ...
- ZOJ 3946 Highway Project 贪心+最短路
题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存 ...
- C++ Primer Plus学习:第十章
过程性编程和面向对象编程 面向对象编程(OOP)的特性: 抽象 封装和数据隐藏 多态 继承 代码的可重用性 抽象和类 类是一种将抽象转化为用户定义类型的C++工具,它将数据表示和操纵数据的方法合成一个 ...
- 七周七语言之用Io编写领域特定语言
如果你想获得更好的阅读体验,可以前往我在 github 上的博客进行阅读,http://lcomplete.github.io/blog/2013/06/05/sevenlang-io/. Io 语言 ...
- 【Nginx】优化配置
nginx优化 突破十万并发 一.一般来说nginx 配置文件中对优化比较有作用的为以下几项: 1. worker_processes 8; nginx 进程数,建议按照cpu 数目来指定,一般为它 ...
- Spring Security 入门详解
序:本文主要参考 spring实战 对里面的知识做一个梳理 1.Spring Security介绍 Spring Security是基于spring的应用程序提供声明式安全保护的安全性框架,它提供了完 ...