J.H


私信TA

用户名:dotcpp0649969

访问量:3217

签 名:

等  级
排  名 78
经  验 9374
参赛次数 1
文章发表 135
年  龄 0
在职情况 学生
学  校 桂林理工大学
专  业 计算机科学与技术

  自我简介:

TA的其他文章

解题思路:

注意事项:

参考代码:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>


typedef struct treenode

{

    char data;

    struct treenode* lchild;

    struct treenode* rchild;

} Tree;


Tree* creattree(char* pre, char* in, int n)

{

    Tree* root;

    int i;

    if (n <= 0)

        return NULL;

    root = (Tree*)malloc(sizeof(Tree));

    if (root == NULL)

        return NULL;

    root->data = pre[0];

    for (i = 0; i < n; i++)

    {

        if (pre[0] == in[i])

            break;

    }

    root->lchild = creattree(pre + 1, in, i);

    root->rchild = creattree(pre + i + 1, in + i + 1, n - i - 1);

    return root;

}


void posttraverse(Tree* root)

{

    if (root != NULL)

    {

        posttraverse(root->lchild);

        posttraverse(root->rchild);

        printf("%c", root->data);

    }

}


int main()

{

    char pre[30], in[30];

    while (scanf("%s%s", pre, in) != EOF)

    {

        Tree* root;

        int n = strlen(pre);

        root = creattree(pre, in, n);

        posttraverse(root);

        printf("\n");

    }

    return 0;

}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区