*/
假设二叉树采用二叉链表存储结构,试设计一个算法
求二叉树的高度并给出指定结点的所在的层数(高度)
*/
# include <iostream>
# include <stdlib.h>
# include <stdio.h>
using namespace std;
typedef struct Bitree
{
char data;
struct Bitree* lchild;
struct Bitree* rchild;
}*bitree;
void createbitree(bitree &t)
{
char ch;
ch = getchar();
if(ch == ' ')
t = NULL;
else
{
t = (bitree) malloc(sizeof(bitree));
t->data = ch;
createbitree(t->lchild);
createbitree(t->rchild);
}
}
int height(bitree t)
{
int a, b, max;
if(t)
{
a = height(t->lchild);
b = height(t->rchild);
max = a>b ? a : b;
return (max+1);
}
else
return 0;
}
int main()
{
bitree t;
printf("请输入以先序建树的字母序列:\n");
createbitree(t);
printf("树的高度为:%d", height(t));
printf("\n");
return 0;
}
**