1086 Tree Traversals Again (两种做法:模拟前序遍历和已知前序中序求树)
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
解题思路:
方法一:模拟前序遍历法
通过观察测试用例可以发现,这和先序遍历有很大的关联,每一次push,就是给当前结点赋值然后递归,每一次pop即返回上一个结点。这样我们可以写出递归遍历函数
nNode* XcreateTree(int arr[]) {
if (arr[nindex] == -1) {
nindex++;
return nullptr;
}
nNode* newNode = new nNode(arr[nindex++]);
if (newNode->lchild == nullptr) {
newNode->lchild = XcreateTree(arr);
}
if (newNode->rchild == nullptr) {
newNode->rchild = XcreateTree(arr);
}
return newNode;
}
我们定义一个数组,每次Push就把值存到数组中,每次pop就设置数组中的元素为-1,递归函数会根据数组中的值判断是继续遍历还是返回上一节点,AC代码如下:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <string.h>
using namespace std;
const int MAXN = 100;
int treeArr[MAXN]; //要用memset来设置
int N;
struct nNode
{
int data;
nNode *lchild, *rchild;
nNode(int _data) {
data = _data;
lchild = rchild = nullptr;
}
};
int nindex = 0;
nNode* XcreateTree(int arr[]) {
if (arr[nindex] == -1) {
nindex++;
return nullptr;
}
nNode* newNode = new nNode(arr[nindex++]);
if (newNode->lchild == nullptr) {
newNode->lchild = XcreateTree(arr);
}
if (newNode->rchild == nullptr) {
newNode->rchild = XcreateTree(arr);
}
return newNode;
}
//后序遍历输出结点
int num = 0;
void XpostOrder(nNode* root) {
if (root->lchild != nullptr) {
XpostOrder(root->lchild);
}
if (root->rchild != nullptr) {
XpostOrder(root->rchild);
}
cout << root->data;
num++;
if (num < N) cout << " ";
}
int main() {
scanf("%d", &N);
memset(treeArr, -1, sizeof(treeArr));
int nnIndex = 0;
int rootData = 0;
char str[5];
int x;
for (int i = 0; i < 2 * N; ++i) {
scanf("%s", &str);
if (strcmp(str, "Push") == 0) {
scanf("%d", &x);
treeArr[nnIndex++] = x;
}
else {
treeArr[nnIndex++] = -1;
}
}
nNode* root = XcreateTree(treeArr);
XpostOrder(root);
system("PAUSE");
return 0;
}
方法二:已知前序中序建树法
通过观察测试用例可以发现,push的顺序为先序遍历顺序,pop的顺序为中序遍历顺序,这样的话,我们就可以把二叉树建立起来
AC代码如下:
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <stack>
using namespace std;
const int MAXN = 100;
struct mmNode
{
int data;
mmNode*lchild, *rchild;
mmNode(int _data) {
data = _data;
lchild = rchild = nullptr;
}
};
int preArrats[MAXN], inArrats[MAXN];
//套用模板即可
mmNode* createTree(int preL, int preR, int inL, int inR) {
if (preL > preR) return nullptr;
int curPreRoot = preArrats[preL];
mmNode* newNode = new mmNode(curPreRoot);
int k;
for (k = inL; k <= inR; ++k) {
if (curPreRoot == inArrats[k]) {
break;
}
}
int numk = k - inL;
newNode->lchild = createTree(preL + 1, preL + numk, inL, k - 1);
newNode->rchild = createTree(preL + numk + 1, preR, k + 1, inR);
return newNode;
}
int N;
int num = 0;
void mmPostOrder(mmNode* root) {
if (root == nullptr) return;
if (root->lchild != nullptr) {
mmPostOrder(root->lchild);
}
if (root->rchild != nullptr) {
mmPostOrder(root->rchild);
}
cout << root->data;
num++;
if (num < N) cout << " ";
}
int main() {
scanf("%d", &N);
char str[5];
int rootdata;
stack<int> simulatorStack;
int preIndex = 0, inIndex = 0;
int x;
for (int i = 0; i < 2 * N; ++i) {
scanf("%s", &str);
if (strcmp(str, "Push") == 0) {
scanf("%d", &x);
preArrats[preIndex++] = x;
simulatorStack.push(x);
}
else {
inArrats[inIndex++] = simulatorStack.top();
simulatorStack.pop();
}
}
mmNode* root = createTree(0, N - 1, 0, N - 1);
mmPostOrder(root);
return 0;
}
- 网站出现“Service Unavailable”提示该如何解决
- Silverlight 4 中摄像头的运用—part1
- Silverlight 4 中摄像头的运用—part1
- Silverlight 4 中数据绑定发生的变化
- 未来3年 人工智能如何影响法律行业?5位权威专家给出趋势
- silverlight.js详解.
- 前端三大框架vue,angular,react大杂烩
- Silverlight制作scrollbar.
- [Silverlight动画]转向行为 - 躲避行为
- WCF后续之旅(1): WCF是如何通过Binding进行通信的
- CaseStudy(showcase)布局篇-列表的排放与遮罩
- [Silverlight动画]转向行为 - 追捕行为
- 前端三大框架vue,angular,react大杂烩
- CaseStudy(showcase)布局篇-如何做一个自适应窗口大小的布局
- 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 数组属性和方法
- TypeScript namespace 命名空间
- 从Pytorch 的ONNX到OpenVINO中IR中间层
- Linux CPU 性能优化指南
- 差分隐私(Differential Privacy)
- ubuntu changelog/source获取方法
- element ui 图片上传封装多张或单张
- Android:友盟分享升级问题总结
- vue+element实现分页的封装
- 一款基于 Python 语言的 Linux 资源监视器!
- 我讨厌这个绿油油的头像!我用opencv换一下背景
- 哈哈哈哈哈哈镜~
- 【对二寸照片的摧残】一:人脸马赛克
- 二寸照片识别/切边/矫正
- 给彦女王生成一副蒙太奇画像
- 设计模式~单例模式