二叉树高度
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef char elemtype;
typedef struct BiTNode
{
elemtype data;
struct BiTNode *lchild, *rchild; /*左右孩子指针*/
} BiTNode,*BiTree;
void CreateBiTree(BiTree *T)
{
//按照先序遍历的节点序列构造二叉树,用空格表示空节点
elemtype ch;
scanf("%c", &ch);
if (ch == '#')
{
*T = NULL;
}
else
{
*T = (BiTree)malloc(sizeof(BiTNode));
(*T)->data = ch;
CreateBiTree(&(*T)->lchild);
CreateBiTree(&(*T)->rchild);
}
}
void Visit(elemtype data)
{
printf("%c", data);
}
void PreOrder(BiTree bt)
{
//先序遍历
if(bt!=NULL)
{
printf("%c",bt->data);
PreOrder(bt->lchild);
PreOrder(bt->rchild);
}
}
int BTHeight(BiTree bt)
{
int lchild,rchild;
if(bt==NULL)
return 0;
else{
lchild=BTHeight(bt->lchild);
rchild=BTHeight(bt->rchild);
return (lchild>rchild)?(lchild+1):(rchild+1);
}
}
int main()
{
BiTree bt;
CreateBiTree(&bt);
int c=BTHeight(bt);
printf("%d",c);
}