zoukankan      html  css  js  c++  java
  • 【例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;
        
    }
  • 相关阅读:
    MySQL sys Schema 简单介绍-2
    dubbo服务+Spring事务+AOP动态数据源切换 出错
    sql 查询优化
    spring事务-说说Propagation及其实现原理
    Redis 分布式锁
    三、操作符
    二、一切皆是对象
    一、对象导论
    SpringMVC工作原理
    数据库性能优化策略
  • 原文地址:https://www.cnblogs.com/EdSheeran/p/8017862.html
Copyright © 2011-2022 走看看