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 = 0;
int top = -1;
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 = 0, Poped = 0;
scanf("%d", &n);
for (int i = 0; i < 2 * n; i++)
{
scanf("%s", operation);
if (strcmp(operation, "Push") == 0)
{
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 = 1;
}
else
{
if (!Poped) //如果前一个操作不是pop,则父节点为栈顶元素
father = Top();
if (father->left == NULL)
father->left = newNode;
else
father->right = newNode;
//printf("%dn", newNode->data);
Push(newNode);
}
Poped = 0;
}
else
{
father = Pop();
Poped = 1;
}
}
PostOrderTraversal(root);
for (int i = 0; i < num-1; i++)
printf("%d ", values[i]);
printf("%dn", values[num-1]);
return 0;
}
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 == -1;
}
treeNode Top()
{
return Stack[top];
}
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 原创 | 险些翻车,差一点没做出来的基础算法题
- 原创 | 你追我,如果你追到我……那就算你赢了
- 原创 | 详解gitignore的使用方法,让你尽情使用git add .
- 第31天:面试比 KMP 还容易被问到的匹配算法!
- 原创 | 深度学习开篇,来聊聊感知机的原理
- 算法题:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字
- 使用Java和Python解题:定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
- R语言中%||%是什么意思?
- 原创 | 你会用缓存吗?详解LRU缓存淘汰算法
- 用Java实现:输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。
- Hacking with iOS: SwiftUI Edition - Hot Prospects项目(一)
- 原创 | 详解command设计模式,解耦操作和回滚
- 第32天:图解大数打印,这道题如此经典!
- Mac终端配置好的环境变量在关闭终端后失效怎么办
- R中的stack和unstack函数