1086. Tree Traversals Again (25)

来源:互联网 发布:四川大学网络学费多少 编辑:程序博客网 时间:2024/06/06 08:56

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

#include<iostream>#include<stack>#include<cstdio>using namespace std;char operate[5];int num,n;stack <int > s;int qian[33],index1=0;int zhong[33],index2=0;int hou[33],index3=0;void fun(int x[],int y[],int len){    if (len<=0) return ;    int index=0;    while(y[index]!=x[0]) index++;    fun(x+1,y,index);    fun(x+index+1,y+index+1,len-index-1);    hou[index3++]=x[0];}int main(){    cin>>n;    for (int i=0;i<2*n;i++)    {        cin>>operate;        if (operate[1]=='u')        {            cin>>num;            s.push(num);            qian[index1++]=num;        }        else        {            num=s.top();            s.pop();            zhong[index2++]=num;        }    }//    for (int i=0;i<6;i++)//        cout<<qian[i];//    for (int i=0;i<6;i++)//        cout<<zhong[i];    fun(qian,zhong ,n);    for (int i=0;i<index3;i++)    {        printf("%d",hou[i]);        if (i!=index3-1)            printf(" ");    }    return 0;}

在牛客网,用如下代码可以AC

#include<iostream>#include<stack>#include<cstdio>using namespace std;struct node{int key,value;};stack<struct node  > s;struct node  qian[330],zhong[303],hou[303];int index1=0,index2=0,index3=0,index4=1;void fun(struct node  x[],struct node  y[],int len){    if (len<=0) return ;    int k=0;    while(y[k].key!=x[0].key)    {        k++;    }    fun(x+1,y,k);    fun(x+k+1,y+k+1,len-k-1);    hou[index3++]=x[0];}int main(){    int n,nn;    char op[33];    scanf("%d",&n);    for (int i=0;i<2*n;i++)    {        cin>>op;        if (op[1]=='u')        {            scanf("%d",&nn);            struct node nm;            nm.key=index4++;            nm.value=nn;            s.push(nm);            qian[index1++]=nm;        }        else        {            zhong[index2++]=s.top();            s.pop();        }    }    fun(qian,zhong,n);    for (int i=0;i<index3;i++)    {        printf("%d",hou[i].value);        if (i!=index3-1)            printf(" ");    }    return 0;}