原题目:
03-树3 Tree Traversals Again (25 分)
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的顺序的是中序遍历的数组的元素;
即 已知 先序遍历和 中序遍历, 求后续遍历;
1. 后续遍历的最后一个元素 等于 先序遍历的第一个元素;
2. 知道先序遍历的第一个元素 val, 则在中序遍历中 ,val元素左边的所有元素全在树的根结点左边,val右边元素全在根结点右边;
3. 由1,2,递归,将子序列的先序遍历第一个元素 赋值给 后序遍历的序列的最后一个位置;
通过的代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int* pre_buf, *in_buf, *post_buf;
//根据输入获取先序遍历,中序遍历的数组元素
void get_buf_val(int total,int ** in_buf, int **pre_buf)
{
int * in_p = *in_buf;
int * pre_p = *pre_buf;
int * tmp_buf = malloc(sizeof(int)*total);
int icount = 0;
char str[10];
int val;
int i = 0, j = 0, pre_Index = 0;
while(icount < total || pre_Index != 0)
{
scanf("%s",str);
if(0 == strcmp(str,"Push"))
{
scanf("%d",&val);
//先序遍历
pre_p[i++] = val;
tmp_buf[pre_Index++] = val;
// pre_Index++;
icount++;
}
else if(0 == strcmp(str,"Pop"))
{
//中序遍历数组
in_p[j++] = tmp_buf[pre_Index-1];
// printf("pre_Index = %d\n",pre_Index);
pre_Index--;
}
}
}
//preL 先序的第一个数的下标 ; inL:中序遍历的第一个数的下标;postL:后续第一个下标
//n: 子序列总数
void solve(int preL, int inL, int postL, int n)
{
if(n==0) return;
if(n==1)
{
post_buf[postL] = pre_buf[preL];
// printf("val:%d\t",post_buf[postL]);
return;
}
int i;
int root = pre_buf[preL];
post_buf[postL + n-1] = root;//后序遍历的最后一个数 是先序的第一个
//找到root在 中序遍历中的位置
for( i = 0; i < n; i++)
if(in_buf[inL + i] == root) break;
int leftNum = i;//左边的数量
int rightNum = n-i-1;
//递归
// printf("l=%d\t",leftNum);
solve(preL+1, inL,postL,leftNum);
// printf("r=%d\t",rightNum);
solve(preL+leftNum+1, inL+leftNum+1,leftNum+postL, rightNum);
}
int main()
{
int itotal;
scanf("%d",&itotal);
pre_buf = malloc(sizeof(int)*itotal);
in_buf = malloc(sizeof(int)*itotal);
post_buf = malloc(sizeof(int)*itotal);
get_buf_val(itotal,&in_buf, &pre_buf);
solve(0,0,0,itotal);
printf("%d",post_buf[0]);
for(int i = 1; i < itotal; i++)
printf(" %d",post_buf[i]);
return 0;
}