六爷 提交的代码
提交时间:2022年5月5日 19:02 语言:C运行时间:0ms占用内存:140K
运行状态: Accepted
题目:二叉树遍历1161

大会员可查看代码,点此开通大会员

                
                    #include <stdio.h>
#include <stdlib.h>

static int ko=0;

typedef struct node{
    char val;
    struct node *LCh;
    struct node *RCh;
}Node,*BTree;

BTree CreatBtree(char ch[], BTree mt){
    if (ch[ko]!='\0'){
        if (ch[ko]=='#'){
            ko++;
            return NULL;
        }      
        mt = malloc(sizeof(Node));
        mt->val = ch[ko++];
        mt->LCh = CreatBtree(ch, mt->LCh);
        mt->RCh = CreatBtree(ch, mt->RCh);
    }
    return mt;
}

void show(BTree mt){
    if (mt!=NULL){
        show(mt->LCh);
        printf("%c ",mt->val);
        show(mt->RCh);
    }
}

int main(){
    char ch[100];
    int i=0;
    BTree mt=NULL;
    while (scanf("%s",&ch)!=EOF){
        if (ch[0]=='0') break;
        ko=0;
        mt = CreatBtree(ch,mt);
        show(mt);
        printf("\n");
    }
    return 0;
}