3341=数据结构实验之二叉树二:遍历二叉树(JAVA)

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 char s[1000];
 5 int t;
 6 struct node
 7 {
 8     char a;
 9     struct node*left,*right;
10 };
11 //这里struct student是类型,*表示是指针,也就
12 //是说函数create()返回值是一个struct student
13 //类型的指针。
14 struct node *creat()
15 {
16     struct node*root;
17     char c=s[t++];
18     if(c!=','){
19         root=(struct node*)malloc(sizeof(struct node));
20         root->a=c;
21         root->left=creat();
22         root->right=creat();
23     }else return NULL;
24     return root;
25 };
26 void mid(struct node*root)
27 {
28     if(root)
29     {
30         mid(root->left);
31         printf("%c",root->a);
32         mid(root->right);
33     }
34 }
35 void after(struct node*root)
36 {
37     if(root)
38     {
39         after(root->left);
40         after(root->right);
41         printf("%c",root->a);
42     }
43 }
44 int main()
45 {
46     struct node*root;
47     while(scanf("%s",s)!=EOF)
48     {
49         t=0;
50         root=(struct node*)malloc(sizeof(struct node));
51         root=creat();
52         mid(root);
53         printf("\n");
54         after(root);
55         printf("\n");
56     }
57     return 0;
58 }

猜你喜欢

转载自www.cnblogs.com/Angfe/p/11705048.html