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

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 char s[1000];
 4 int top;
 5 struct node
 6 {
 7     char c;
 8     struct node *left, *right;
 9 };
10 struct node * creat()
11 {
12     struct node * root;
13     char c = s[top++];
14     if(c!=',')
15     {
16         root = (struct node *)malloc(sizeof(struct node));
17         root->c=c;
18         root->left=creat();
19         root->right=creat();
20     }
21     else
22         return NULL;
23     return root;
24 };
25 void mid(struct node *root)
26 {
27     if(root)
28     {
29         mid(root->left);
30         printf("%c",root->c);
31         mid(root->right);
32     }
33 }
34 void back(struct node *root)
35 {
36     if(root)
37     {
38         back(root->left);
39         back(root->right);
40         printf("%c",root->c);
41     }
42 }
43 int main()
44 {
45     while(~scanf("%s",s))
46     {
47         top=0;
48         struct node * root;
49         root = (struct node *)malloc(sizeof(struct node));
50         root=creat();
51         mid(root);
52         printf("
");
53         back(root);
54         printf("
");
55     }
56     return 0;
57 }
原文地址:https://www.cnblogs.com/Angfe/p/11838759.html