利用递归求一个二叉树的高度
typedef struct BiNode
{
Datatype data;
BiNode *left;
BiNode *right;
};
int treedepth(BiNode *root)
{
int hl,hr;
if(root)
{
if(root->left==NULL&&root->right==NULL)
return 1;
else
{
hl=return treedepth(root->left);
hr=return treedepth(root->right);
return (hl>hr ? hl+1 : hr+1);
}
}
else
return 0;
}
本文介绍了一种使用递归方法来计算二叉树高度的算法实现。通过定义二叉树节点结构,并利用递归函数treedepth计算左子树和右子树的最大深度,进而得出整个二叉树的高度。
1万+

被折叠的 条评论
为什么被折叠?



