输出以二叉树表示的算术表达式(严6.51)--------西工大noj

 题解

这道题目说的很诡异,其实没有什么把括号补上。。。。仅仅是先序读入,然后中序输出就行了

代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct TreeNode
{
    char data;
    struct TreeNode *LChild, *RChild;

}TreeNode;
char *c;
void Create(TreeNode **T)
{
    if(*c=='#')
    {
        (*T) = NULL;
        c++;
        return ;
    }
    (*T) = (TreeNode*)malloc(sizeof(TreeNode));
    (*T)->data = *c;
    c++;
    Create(&(*T)->LChild);
    Create(&(*T)->RChild);
}
void Print(TreeNode*T)
{
    if(!T)
        return;
    Print(T->LChild);
    printf("%c",T->data);
    Print(T->RChild);
}
int main()
{
    static char buf[10000];
    scanf("%s",buf);
    c = buf;
    TreeNode *T;
    Create(&T);
    Print(T);
    return 0;
}
/*
*+a(###b#)##c##
*/

posted @ 2022-07-02 19:00  心坚石穿  阅读(54)  评论(0)    收藏  举报