根据中序序列和前序序列,求二叉树的后序序列

来源:互联网 发布:我国网络舆论的特点是? 编辑:程序博客网 时间:2024/05/22 01:43
/*
作者:shaoshaoh
日期:2006-10-01
声明:欢迎转载,请保留此信息

Problem C: Mr. Bean

Mr. Bean likes bean games, especially the "beanary tree" game. To enjoy a 
beanary tree game, he plays the following steps:
l   He firstly picks one bean from each of his N different kinds of beans 
and randomly arranges them into a sequence, that is, a sequence of N 
different beans. Mr. Bean calls this sequence the preorder sequence.
l   Then, he picks another random bean sequence in the same way, called the 
inorder sequence.(strange names, aren't they?)
l   Now come to the difficult step. Mr. Bean picks yet another N different 
beans and, this time, he tries to put them into…guess what, no no, not a 
sequence, but a tree! A beanary-tree! 
What is a beanary-tree? A beanary-tree is in fact a special kind of binary 
tree of beans. Such tree has the magic property that if you picks its beans 
in pre-order(recursively parent then left then right), the bean sequence 
thus obtained is just the same as the preorder sequence he randomly picked 
in the first step! Besides, if you pick the beans in in-order(recursively 
left then parent then right), the bean sequence thus obtained is just the 
same as the inorder sequence! What a magic. 
l   This is not the end of the game. Mr. Bean finally picks the beans in 
postorder(recursively left then right then parent) and record the resulted 
postorder sequence in his notebook.(sequences save space, unlike trees…)

 
This figure illustrates the preorder, inorder, postorder of a binary tree

The game is difficult. Sometimes it's even impossible to finish the 
process. Usually, the old Bean found that he is too old for doing this, so 
he asks you for help. Please write a clever program that could input the 
preorder sequence and the inorder sequence and then play the rest of the 
game. To make your life easier, you only need to output the corresponding 
postorder sequence if possible.

Input Specification:
The input only contains two strings. The first string represents the 
preorder sequence and the second string represents the inorder sequence. 
Each of the strings consists only of capital letters. Different capital 
letters represent different kinds of beans. You may assume that the input 
is always meaningful for the game. 

Output Specification:
In a single line, output a string representing the postorder sequence. If 
it is impossible, output the string "-_-bbb"(no quotation mark should be 
printed).

Sample Input 1:
KGCBDHQMPY
BCDGHKMPQY

Sample Output 1:
BDCHGPMYQK

Sample Input 2:
ABC
CAB

Sample Output 2:
-_-bbb
*/


#include 
<stack>
#include 
<iostream>
using namespace std;


char pre[100], in[100];
stack
<char> tt;
bool flag;

int search(char* arr, char key, int s, int e)
{
    
int pos = s; 
    
while(arr[pos]!=key && pos<=e)
    
{
        pos
++;
    }
;
    
if (pos>e) return -1;
    
else return pos;
}


void pushall(char *pre, int ps, int pe, char *in, int ins, int ine)
//给出一个前序序列,给出一个中序序列,然后根据中序和前序,按照后序的
//逆序,将队列压栈。也即先将根节点压栈,然后对于右子树和左子树递归
//调用此过程。最后实现将全部节点值压栈,逐个弹出就是后序遍历的结果
{
    flag 
= false;
    tt.push(pre[ps]);
    
if (ps == pe)
    
{
        
if (pre[ps] == in[ins])
            flag 
= true;
        
return;
    }

    
int i = search(in, pre[ps], ins, ine);
    
if (i==-1return;

    
//如果存在右子树,递归
    if (ps+(i-ins) < pe) pushall(pre,ps+(i-ins)+1,pe,in,i+1,ine);
    
//如果存在左子树,递归
    if (i-ins > 0) pushall(pre,ps+1,ps+(i-ins),in, ins, i-1);
}



void main(void)
{
    flag 
= false;
    cout
<<"Please input the preorder list"<<endl;
    cin
>>pre;
    cout
<<"Please input the inorder list"<<endl;
    cin
>>in;
    
int len = 0;
    
while(pre[len]!=0)
    
{
        len
++;
    }

    
    pushall(pre,
0,len-1,in,0,len-1);


    
if (!flag)
    
{
        cout
<<"-_-bbb"<<endl;
        
return;
    }


    
while(!tt.empty())
    
{
        cout
<<tt.top();
        tt.pop();
    }

    cout
<<endl;
}

 
原创粉丝点击