美文网首页数据结构和算法分析
先序,中序序列 推导后序序列

先序,中序序列 推导后序序列

作者: Co_zy | 来源:发表于2018-08-16 14:17 被阅读0次
    Problem Description

    输入二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列。

    Input

    第一行输入二叉树的先序遍历序列;
    第二行输入二叉树的中序遍历序列。

    Output

    输出该二叉树的后序遍历序列。

    Sample Input

    ABDCEF
    BDAECF

    Sample Output

    DBEFCA

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct BTNode
    {
        char data;
        struct BTNode *left,*right;
    }BTNode;
    
    void createBT(char *pre,char *in,int len)
    {
        if(len == 0)
            return;
        BTNode *t = (BTNode *)malloc(sizeof(BTNode));
        t->data = *pre;
        int i;
        for(i=0;i<len;i++)
        {
            if(in[i] == *pre)
                break;
        } 
        createBT(pre+1,in,i);
        createBT(pre+i+1,in+i+1,len-i-1);
        printf("%c",t->data);
        
    }
    
    int main()
    {
        char pre[1000];
        char in[1000];
        gets(pre);
        gets(in);
        int len = strlen(in);
        createBT(pre,in,len);
        printf("\n");
        return 0;
    }
    

    相关文章

      网友评论

        本文标题:先序,中序序列 推导后序序列

        本文链接:https://www.haomeiwen.com/subject/httobftx.html