1086. Tree Traversals Again (25)

来源:互联网 发布:网络交易平台怎么做 编辑:程序博客网 时间:2024/04/30 07:01

1086. Tree Traversals Again (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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:
6Push 1Push 2Push 3PopPopPush 4PopPopPush 5Push 6PopPop
Sample Output:

3 4 2 6 5 1

这是一道比较常见的树的题目,题意其实是push的顺序代表了前序序列,pop之后的序列是代表终须序列,最后求出后序序列。

题中的前序序列是1 2 3 4 5 6,中序是3 2 4 1 6 5,

由于前序的第一个数是树的根节点,通过找到中序中该节点的位置,如1是根节点,在中序的位置为3,那3的左边是左子树,右边为右子树,同时对应前序也找到了234为左子树,56是右子树。那如此递归就可找到每棵树的根节点,最后按顺序放入数组就是后序序列。我的实现是用一个函数findpost(int in_a,int in_b , int pre_a, int  pre_b),来递归计算。知道每一棵树为1个节点时停止递归。具体代码如下:

#include <stdlib.h>#include<stdio.h>#include<string.h>using namespace std;int pre[40] , inorder[40];int post[40],num_post=0;int findind(int des,int num[40] ,int a, int b){    int i;    for( i =a ;i <=b ;i++){        if( num[i] ==des)            return i;    }}void findpost(int in_a,int in_b , int pre_a, int  pre_b){    int k_pre,k_in , ka , kb;   // post[in_b] = pre[pre_a];    if(in_a == in_b){        post[num_post++] = pre[pre_a];       return ;    }       else{        k_in = findind(pre[pre_a],inorder, in_a, in_b);//printf("%d\n",k_in);        ka= k_in - in_a;        kb= in_b-k_in;        if(ka>0)          findpost(in_a,k_in-1,pre_a+1, pre_a+ ka);        if(kb>0)          findpost(k_in+1,in_b, pre_b-kb+1,pre_b);       post[num_post++] = pre[pre_a];       }}int main(){    int n , i, j ,num_pe=0,num_in=0;    char getin[10];    int stackn[70],num_stack = 0;    scanf("%d" ,&n);    for( i =0 ;i< 2*n; i++){        scanf("%s",getin);//printf("%d %s\n",i,getin);        if(strlen(getin)>3){            scanf("%d",&pre[num_pe]);//printf("%d\n",pre[num_pe]);            stackn[num_stack++] = pre[num_pe];            num_pe++;        }        else{            inorder[num_in++] = stackn[--num_stack];        }    }    findpost(0,n-1, 0 ,n-1);    for( i =0 ;i <n-1; i++)        printf("%d ",post[i]);     printf("%d\n",post[n-1]);    return 0;}



0 0
原创粉丝点击