题解 | #二叉树遍历#

二叉树遍历

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

#include <stdio.h>
#include <stdlib.h>
typedef char BTDataType;
typedef struct BinaryNode
{
    BTDataType data;
    struct BinaryNode* left;
    struct BinaryNode* right;
}BTNode;

BTNode* BuyNode(BTDataType val)
{
    BTNode* newNode = (BTNode*)malloc(sizeof(BTNode));
    if(newNode == NULL)
    {
        perror("malloc fail");
        return NULL;
    }
    newNode->data=val;
    newNode->left=NULL;
    newNode->right=NULL;

    return newNode;
}

BTNode* OrderBinaryCreateHelper(BTDataType* a,int* pi)
{
    if(a[*pi] == '#')
    {
        (*pi)++;
        return NULL;
    }
    BTNode* newNode = BuyNode(a[(*pi)++]);
    newNode->left = OrderBinaryCreateHelper(a,  pi);
    newNode->right=OrderBinaryCreateHelper(a,  pi);

    return newNode;
}

void BTreeQrder(BTNode* root)
{
    if(root ==NULL)
        return ;
    BTreeQrder(root->left);
    printf("%c ",root->data);
    BTreeQrder(root->right);
    
}

int main() 
{
    char a[101];
    scanf("%s",a);
    int Index = 0;
    BTNode* root = OrderBinaryCreateHelper(a, &Index);
    BTreeQrder(root);
    return 0;
}

全部评论

相关推荐

10-15 10:57
已编辑
武昌理工学院 FPGA工程师
狠赚笔第一人:老哥学院本没实习还想拿13k学Java狠赚笔呢
点赞 评论 收藏
分享
评论
1
收藏
分享
牛客网
牛客企业服务