#include<stdio.h>#include<stdlib.h>#include<malloc.h> typedef int ElemType;//数据类型//定义二叉树结构,与单链表相似,多了一个右孩子结点typedef struct BiTNode{ ElemType data; struct BiTNode *lChild,*rChild;}BiTNode,*BiTree; //先序创建二叉树int CreateBiTree(BiTree *T){ ElemType ch; ElemType temp; scanf("%d",&ch); temp=getchar(); if(ch==-1) { *T=NULL; } else { *T=(BiTree)malloc(sizeof(BiTNode)); if(!(*T)) { exit(-1); } (*T)->data=ch; printf("输入%d的左子结点:",ch); CreateBiTree(&(*T)->lChild); printf("输入%d的右子结点:",ch); CreateBiTree(&(*T)->rChild); } return 1;} //先序遍历二叉树void TraverseBiTree(BiTree T){ if(T==NULL) { return; } printf("%d",T->data); TraverseBiTree(T->lChild); TraverseBiTree(T->rChild);} //中序遍历二叉树void InOrderBiTree(BiTree T){ if(T==NULL) { return; } InOrderBiTree(T->lChild); printf("%d",T->data); InOrderBiTree(T->rChild);} //后序遍历二叉树void PostOrderBiTree(BiTree T){ if(T==NULL) { return; } PostOrderBiTree(T->lChild); PostOrderBiTree(T->rChild); printf("%d",T->data);} //二叉树的深度int TreeDeep(BiTree T){ int deep=0; if(T) { int leftdeep=TreeDeep(T->lChild); int rightdeep=TreeDeep(T->rChild); deep=leftdeep>=rightdeep?leftdeep+1:rightdeep+1; } return deep;} //求二叉树叶子结点个数int Leafcount(BiTree T,int &num){ if(T) { if(T->lChild==NULL&&T->rChild==NULL) { num++; } Leafcount(T->lChild,num); Leafcount(T->rChild,num); } return num;} //主函数int main(void){ BiTree T; BiTree *p=(BiTree *)malloc(sizeof(BiTree)); int deepth,num=0; printf("请输入第一个结点的值,-1表示没有叶结点:/n"); CreateBiTree(&T); printf("先序遍历二叉树:/n"); TraverseBiTree(T); printf("/n"); printf("中序遍历二叉树:/n"); InOrderBiTree(T); printf("/n"); printf("后序遍历二叉树:/n"); PostOrderBiTree(T); printf("/n"); deepth=TreeDeep(T); printf("数的深度为:%d",deepth); printf("/n"); Leafcount(T,num); printf("数的叶子结点个数为:%d",num); printf("/n"); return 0;}