二叉树遍历

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

typedef struct tree {
//    char data;
int data;
struct tree *left;
struct tree *right;
}tree;

tree* CreateBTree(){
tree *t;
//    char ch;
int ch;
scanf_s(“%d”, &ch);
printf(“\n”);
if (ch == 0)
t = NULL;
else {
t = (tree *)malloc(sizeof(tree));
if (!t)
exit(-1);
t->data = ch;
printf(“Node %d left child: “, t->data);
t->left = CreateBTree();
printf(“Node %d right child: “, t->data);
t->right = CreateBTree();
}
return t;
}

int First(tree *n){
if (!n)
return 0;
else {
printf(“%d “, n->data);
First(n->left);
First(n->right);
}
return 0;
}

int Last(tree *n){
if (!n)
return 0;
else {
Last(n->left);
Last(n->right);
printf(“%d” , n->data);
}
return 0;
}

int Middle(tree *n){
if (!n)
return 0;
else {
Middle(n->left);
printf(“%d” , n->data);
Middle(n->right);
}
return 0;
}

int main(){
tree *p;
p = (tree *)malloc(sizeof(tree));
if (!p)
exit(-1);
printf(“Create BTree !\n”);
printf(“Please input root node: “);
p = CreateBTree();
printf(“\nFirst Visit BTree: “);
First(p);
printf(“\nLast Visit BTree: “);
Last(p);
printf(“\nMiddle Visit BTree: “);
Middle(p);
//    printf(“Step Visit BTree: “);
//    Step(p);
return 0;
}