题解 | #二叉树遍历#

二叉树遍历

http://www.nowcoder.com/practice/4b91205483694f449f94c179883c1fef

C语言实现

#include <stdlib.h>
#include <assert.h>

typedef struct BinTree{
    char data;
    struct BinTree *left;
    struct BinTree *right;
}BinTree;
typedef struct BinTree *BTree;
BTree CreateTree(const char *s,int *i)
{
    if(s[*i]=='#' || s[*i]=='\0')
        return NULL;
    else{
        BinTree *t=(BinTree *)malloc(sizeof(BinTree));
        assert(t);
        t->data=s[*i];
        (*i)++;
        t->left=CreateTree(s,i);
        (*i)++;
        t->right=CreateTree(s,i);
        return t;
    }
    
}
void BTreeLVR(BTree bt)
{
    if(bt!=NULL){
    BTreeLVR(bt->left);
    printf("%c ",bt->data);
    BTreeLVR(bt->right);
    }
}
int main()
{
    BTree bt;
    char s[100]={0};
    scanf("%s",s);
    int i=0;
    bt=CreateTree(s, &i);
    BTreeLVR(bt);
    return 0;
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务