题解 | #二叉树遍历#

二叉树遍历

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

#include "stdio.h"
#include "stdlib.h"

typedef struct BTNode {
    // 数据域
    char data;
    // 指针域
    struct BTNode* left_child;
    struct BTNode* right_child;
} BTNode, *BTree;

BTree preCreateTree(char* input, int* pi) {
    if ('#' == input[*pi]) {
        (*pi)++;
        return NULL;
    }
    BTNode* root = (BTNode*) malloc(sizeof(BTNode));
    root->data = input[*pi];
    (*pi)++;
    root->left_child = preCreateTree(input, pi);
    root->right_child = preCreateTree(input, pi);
    return root;
}

void inOrder(BTNode* node) {
    if (NULL == node)
        return;
    inOrder(node->left_child);
    printf("%c ", node->data);
    inOrder(node->right_child);
}

int main() {
    char buf[1024];
    struct BTNode* root = NULL;
    while (EOF != scanf("%s", buf)) {
        int index = 0;
        root = preCreateTree(buf, &index);
        inOrder(root);
        printf("\n");
        free(root);
    }
    return 0;
}

全部评论

相关推荐

07-01 17:14
中北大学 Java
兄弟们是真是假
牛客46374834...:我在boss上投java岗从来没成功过
点赞 评论 收藏
分享
06-12 10:50
门头沟学院 Java
你的不定积分没加C:我怎么在学院群看到了同样的话
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务