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.
二叉树的遍历可利用堆栈通过非递归的方式实现。例如,当一个含有六个节点的二叉树被遍历时,堆栈操作为 push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop().然后通过这系列操作就能生成一个特定的二叉树。你的任务是给出这个二叉树的后序遍历序列。
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.
对于每个测试案例,第一行给一个正整数N表示树的总结点数,然后2N行,每一行按如下形式描述一个堆栈操作:Push X,X是压栈的结点的值;Pop,从堆栈中出栈一个结点。
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序列是中序遍历,所以就变成了根据前序遍历和中序遍历求二叉树,这种方法老师在课堂上讲过了是完全可行的,所以可以采用这种方法。
而我想的是能不能通过不建立二叉树直接求后序遍历序列。然后想到了老师的这页PPT。前序遍历是第一次访问,中序遍历是第二次访问,后序遍历是第三次访问,而对应到堆栈操作中,Push压栈是第一次访问,Pop出栈是第二次访问,想得到后序遍历序列就得增加第三次访问,所以我们给每个结点加一个标记位1,每个结点第一次Pop时不出栈而是把标记位的值变为2,在下一次Pop时才把标记位为2的结点出栈(此时即为第三次访问),这样再配合一些辅助数组存储内容,就能不建立二叉树直接得到我们的后序遍历序列了。
代码如下
编程环境 VS2017
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int ElementType;
typedef int Position;
typedef struct SNode *Stack;
#define null -1
struct SNode
{
ElementType Data;
int tag;
struct SNode *Next;
};
Stack CreateStack() {
Stack S;
S = (Stack)malloc(sizeof(struct SNode));
S->Next = NULL;
return S;
}
int IsEmpty(Stack S) {
return (S->Next == NULL);
}
void Push(ElementType item, Stack S) {
struct SNode *Tmpcell;
Tmpcell = (struct SNode *)malloc(sizeof(struct SNode));
Tmpcell->Data = item;
Tmpcell->tag = 1;
Tmpcell->Next = S->Next;
S->Next = Tmpcell;
}
ElementType Pop(Stack S) {
struct SNode *FirstCell;
ElementType TopElem;
if(IsEmpty(S)) {
return null;
}
else
{
FirstCell = S->Next;
S->Next = FirstCell->Next;
TopElem = FirstCell->Data;
free(FirstCell);
return TopElem;
}
}
int main() {
int N,m,cnt=0,num[30];
ElementType tmp;
char ch[10];
Stack S;
scanf("%d\n", &N);
S = CreateStack();
for (int i = 0; i < 2*N; i++)
{
scanf("%s", ch);
if (ch[1]=='u')
{
scanf("%d\n", &m);
Push(m, S);
}
else
{
while (S->Next->tag == 2)
{
tmp = Pop(S);
if(tmp!=null) num[cnt++] = tmp;
}
if(!IsEmpty(S)) S->Next->tag = 2;
}
}
while (!IsEmpty(S))
{
tmp = Pop(S);
if (tmp != null) num[cnt++] = tmp;
}
for (int i = 0; i < N; i++)
{
printf("%d", num[i]);
if (i < N - 1) printf(" ");
}
//system("pause");
return 0;
}
对你有帮助的话记得点赞哦!