zoukankan      html  css  js  c++  java
  • 数据结构实验之求二叉树后序遍历和层次遍历 分类: 树 2015-06-21 10:58 11人阅读 评论(0) 收藏

    数据结构实验之求二叉树后序遍历和层次遍历
    Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
    题目描述
    已知一棵二叉树的前序遍历和中序遍历,求二叉树的后序遍历。
    输入
    输入数据有多组,第一行是一个整数t (t<1000),代表有t组测试数据。每组包括两个长度小于50 的字符串,第一个字符串表示二叉树的先序遍历序列,第二个字符串表示二叉树的中序遍历序列。
    输出
    每组第一行输出二叉树的后序遍历序列,第二行输出二叉树的层次遍历序列
    示例输入

    2
    abdegcf
    dbgeafc
    xnliu
    lnixu

    示例输出

    dgebfca
    abcdefg
    linux
    xnuli

    /*
    建立二叉树,进行层次遍历和前,中,后序的遍历
    */
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <cstdlib>
    #include <time.h>
    #include <cctype>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <climits>
    #include <string>
    #include <iostream>
    #include <algorithm>
    #define RR freopen("input.txt","r",stdin)
    #define WW freopen("output.txt","w",stdout)
    #define INF 0x3f3f3f3f
    
    using namespace std;
    
    const int Max=1000001;
    
    char head[110];
    
    char mid[110];
    struct Tree
    {
        char c;
        Tree *L;
        Tree *R;
    };
    
    struct Tree * Creat()
    {
        Tree *p;
        p=new Tree;
        p->L=NULL;
        p->R=NULL;
        return p;
    }
    struct Tree* Build(int low,int high,int star,Tree *root)//根据前序和中序建树
    {
        if(low>high)
        {
            return NULL;
        }
        int i;
        for( i=low;i<=high;i++)
        {
            if(head[star]==mid[i])
            {
                break;
            }
        }
        root=Creat();
        root->c=mid[i];
        root->L=Build(low,i-1,star+1,root->L);
        root->R=Build(i+1,high,star+i-low+1,root->R);
        return root;
    }
    void Output(Tree *root)//输出
    {
        if(!root)
            return ;
        Output(root->L);
        Output(root->R);
        printf("%c",root->c);
    }
    void BFS(Tree *root)//层次遍历
    {
        queue<Tree *>B;//借助队列
        Tree *p;
        B.push(root);
        while(!B.empty())
        {
            p=B.front();
            B.pop();
            printf("%c",p->c);
            if(p->L)
            {
                B.push(p->L);
            }
            if(p->R)
            {
                B.push(p->R);
            }
        }
    }
    int main()
    {
        int n;
        scanf("%d",&n);
        while(n--)
        {
            Tree * Root;
            scanf("%s %s",head,mid);
            int len=strlen(mid);
            Root=Build(0,len-1,0,Root);
            Output(Root);
            cout<<endl;
            BFS(Root);
            cout<<endl;
        }
        return 0;
    }
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    保留两位小数 只能数字或小数 只能有一个小数点并且第一位不能为小数点的正则表达式
    Vue数据改变页面不更新 使用this.$set()强制更新的用法
    给对象新增属性
    vue-element的<el-tooltip>气泡提示失效问题
    vue刷新更新子组件
    watch监控对象
    vue-element-admin的api的js文件 get, post, put, delelt传参
    git 还原到某个版本
    在64位Win7系统中配置jsp开发环境以及一些问题解决方案
    在64位Win7系统中配置Apache+Mysql+PHP环境以及一些问题解决方案
  • 原文地址:https://www.cnblogs.com/juechen/p/4722002.html
Copyright © 2011-2022 走看看