【例3-5】扩展二叉树

【例3-5】扩展二叉树

链接:http://ybt.ssoier.cn:8088/problem_show.php?pid=1340
时间限制: 1000 ms         内存限制: 65536 KB

【题目描述】

由于先序、中序和后序序列中的任一个都不能唯一确定一棵二叉树,所以对二叉树做如下处理,将二叉树的空结点用·补齐,如图所示。我们把这样处理后的二叉树称为原二叉树的扩展二叉树,扩展二叉树的先序和后序序列能唯一确定其二叉树。

现给出扩展二叉树的先序序列,要求输出其中序和后序序列。

【输入】

扩展二叉树的先序序列。

【输出】

输出其中序和后序序列。

【输入样例】

ABD..EF..G..C..

【输出样例】

DBFEGAC
DFGEBCA
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
typedef struct node;
typedef node *tree;
struct node{
    char data;
    tree l,r,f;
};
tree bt;
int i=-1;
string s;
void build(tree &bt)
{
    
    if(s[++i]!='.')
    {
        bt=new node;
        bt->data=s[i];
    
        build(bt->l);
        build(bt->r);
        
    }
    else bt=NULL;
}
void post(tree bt)
{
    if(bt)
    {
        post(bt->l);
        post(bt->r);
        cout<<bt->data;
    }
}
void mid(tree bt)
{
    if(bt)
    {
        mid(bt->l);
        cout<<bt->data;
        mid(bt->r);
        
    }
}
int main()
{
    
    cin>>s;
    
    build(bt);
    mid(bt);
    cout<<endl;
    post(bt);
    cout<<endl;
    
}
原文地址:https://www.cnblogs.com/EdSheeran/p/8017862.html