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;
}