#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef char elemtype;
typedef struct BiTNode{
elemtype data;
struct BiTNode *lchild; //左孩子指针
struct BiTNode *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); //递归创建右分支
}
}
int Depth(BiTree T)
{
int ld=0,rd=0;
if(T==NULL)
{
return 0;
}
else
ld=Depth(T->lchild);
rd=Depth(T->rchild);
if(ld>rd)
return(ld+1);
else
return(rd+1);
}
int main()
{
BiTree bt;
CreateBiTree(&bt);//构造二叉树
int a=Depth(bt);
printf("%d",a);
}
【代码运行】计算二叉树深度
最新推荐文章于 2023-09-07 09:37:26 发布