I study the data structure,
met a strange problem.
I used preorder method to create a binary tree;
code :
Now I want to traverse the tree,and put out the result---Code:#include<stdio.h> typedef struct node { int data; struct node *left; struct node *right;}btree; btree *Binarytree(btree *bt) { int data; scanf("%d",&data); if(data==0) bt=NULL; else { bt=(btree *)malloc(sizeof(btree)); bt->data=data; Binarytree(bt->left); Binarytree(bt->right); } return bt; } main() { btree *bt; bt=(btree *)malloc(sizeof(btree)); printf("\n\t\tThe Binary Tree:\n"); printf("\nPlease input the data of binary tree(0 for NULL) :\n"); Binarytree(bt); }
each node's data;
And the problem comes, when I traverse,it can't traverse the
left child and right child;
Thanx very much!