程序代码:
#include <stdio.h> #include <stdlib.h> typedef struct node { int data; struct node *l_child; struct node *r_child; }*BTree; BTree create(BTree tree) { int temp; scanf("%d", &temp); if (temp == 0) { tree = NULL; } else { tree = (BTree) malloc (sizeof(struct node)); tree->l_child = create(tree->l_child); tree->r_child = create(tree->r_child); } return tree; } BTree init(BTree tree) { if ( tree != NULL ) { tree->l_child = init(tree->l_child); scanf("%d", &tree->data); tree->r_child = init(tree->r_child); } return tree; } void show(BTree tree) { if (tree != NULL) { show(tree->l_child); printf("%d ", tree->data); show(tree->r_child); } } int main(void) { BTree tree = NULL; tree = create(tree); printf("\n"); tree = init(tree); show(tree); printf("\n"); return 0; }