同学突然向我问二叉树的三种遍历代码。数据结构刚刚学了,自己很吃力的敲了出来。
和老师演示的代码有很大差距。
#include <stdio.h>#include <string.h>#include <stdlib.h>#define Error -1#define Right 1struct BiTnode{ char data; struct BiTnode *LChild; struct BiTnode *RChild;};BiTnode *Creat_BinaryTree(){ BiTnode *t; t=(BiTnode *)malloc(sizeof(BiTnode)); t->data=getchar(); if(t->data=='1') { t=NULL; return t; } t->LChild=Creat_BinaryTree(); t->RChild=Creat_BinaryTree(); return t;}void Preorder(BiTnode * t){ if(t) { putchar(t->data); Preorder(t->LChild); Preorder(t->RChild); }}void Midorder(BiTnode * t){ if(t) { Midorder(t->LChild); putchar(t->data); Midorder(t->RChild); }}void Posorder(BiTnode * t){ if(t) { Posorder(t->LChild); Posorder(t->RChild); putchar(t->data); }}int main(){ BiTnode * t; t=Creat_BinaryTree(); printf("前序遍历为:"); Preorder(t); putchar('\n'); printf("中序遍历为:"); Midorder(t); putchar('\n'); printf("后续遍历为:"); Posorder(t); putchar('\n'); free(t); return 0;}
你在无垠的海边第一次听到了自己心跳的声音,