题解 | #二叉树遍历#
二叉树遍历
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; }