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的为二叉树的中序遍历序列,要根据先序序列和中序序列求后序序列,愚蠢的我一时没有发现,于是用了错误的方法:
这个算法明显有很大问题,只通过了一个测试样例,稍加验证就会发现很多情况是无法正确输出的。而且输入带有空格的字符串,用cin.getline仿佛还出了问题,必须2N+1的循环才能输出2N项。
#include<iostream>
#include<string>
const int MaxSize = 50;
using namespace std;
typedef int ElemType;
typedef struct {
ElemType data[MaxSize];
int top;
}SqStack;
void InitStack(SqStack*& s)
{
s = new SqStack;
s->top = -1;
}
void DestroyStack(SqStack*& s)
{
delete s;
}
bool stackEmpty(SqStack* s)
{
return(s->top == -1);
}
void Push(SqStack*& s, ElemType e)
{
if (s->top == MaxSize - 1)
return;
s->top++;
s->data[s->top] = e;
}
void Pop(SqStack* s, ElemType& e)
{
if (s->top == -1)
return ;
e = s->data[s->top];
s->top--;
}
int main()
{
int N;
cin >> N;
int x;
SqStack *q;
InitStack(q);
int a[50] = { 0 };
int flag = 0;
char s[10];
int ans = -1;
for (int i = 0;i <= 2 * N;i++)
{
cin.getline(s,10);
if (s[1] == 'u')
{
x = s[5] - '0';
Push(q, x);
}
if (s[1] == 'o')
{
ans++;
}
while (ans > 0)
{
Pop(q, a[flag++]);
ans--;
}
}
Pop(q, a[flag++]);
for (int i = 0;i < flag-1;i++)
cout << a[i] << " ";
cout << a[flag - 1];
}
后来观摩了姥姥的做法,豁然开朗
这里使用了stack头文件简化运算,然后输入也分两步进行,先输入一个string类,判断是不是Push,然后再输入数字。巧妙的化解了cin只识别到空格就结束的一个弊端。而且没有出现cin.getline出现的问题。
#include<iostream>
#include<stack>
#include<string>
using namespace std;
const int MaxSize = 100;
int pre[MaxSize], in[MaxSize], post[MaxSize];
void solve(int PreL, int inL, int postL, int n) {
if (n == 0)return;
if (n == 1) {
post[postL] = pre[PreL];
return;
}
int i;
int root = pre[PreL];
post[postL + n - 1] = root;
for (i = 0; i < n; i++) {
if (in[inL + i] == root)break;
}
int L = i, R = n - i - 1;
solve(PreL + 1, inL, postL, L);
solve(PreL + 1 + L, inL + L + 1, postL + L, R);
}
int main()
{
int N, num, i = 0, j = 0;
cin >> N;
string s;//这里不要轻易使用cin.getline函数,会出问题
stack<int> st;
for (int z = 0; z < 2 * N; z++) {
cin >> s;
if (s[1] =='u') {
cin >> num;
st.push(num);
pre[i++] = num;
}
else {
num = st.top();
st.pop();
in[j++] = num;
}
}
solve(0, 0, 0, N);
for (int i = 0;i < N - 1;i++)
cout << post[i] << " ";
cout << post[N - 1];
return 0;
}
要特别注意姥姥这里的递归函数的使用以及含有空格字符串的输入。