中序遍历二叉树
#include<stdio.h> #include<stdlib.h> //用二叉链表存储方式建树(完全二叉树) typedef struct BitTree { int data; struct BitTree* LChild; //左子树 struct BitTree* RChild; //右子树 }bittree; //创建二叉树 bittree* createBitTree(bittree* BT) { int num = 0; scanf("%d", &num); if (num != -1) { //输入-1代表结束 BT = (bittree*)malloc(sizeof(bittree)); BT->data = num; printf("输入%d的左结点值:", BT->data); BT->LChild = createBitTree(BT->LChild); printf("输入%d的右结点值:", BT->data); BT->RChild = createBitTree(BT->RChild); } else { BT = NULL; //输入-1,结点为NULL } return BT; } //中序遍历 void MidPrint(bittree* BT) { if (BT != NULL) { MidPrint(BT->LChild); printf("%d ", BT->data); MidPrint(BT->RChild); } else { //结点为NULL,返回上一层 return; } } void main() { bittree* myBT = NULL; myBT = createBitTree(myBT); printf("中序遍历二叉树:\n"); MidPrint(myBT); }