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("\n"); 53 back(root); 54 printf("\n"); 55 } 56 return 0; 57 }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步