二叉树为空:深度为0;
二叉树为0:深度为1;
一般的二叉树:深度=max{左子树的深度,右子树的深度} + 1。
int Depth (BiTree T)
{
if (!T)//如果二叉树根节点为空,则深度为0
depthval=0;
else
{depthLeft = Depth(T->lchild); //左子树的深度
depthRight = Depth(T->rchild);//右子树的深度
depthval = 1 +(depthLeft>depthRight?depthLeft:depthRight);
}
return depthval;
}
本文介绍了一种递归算法来计算二叉树的深度。当二叉树为空时,其深度定义为0;若仅有一个节点,则深度为1;对于一般情况的二叉树,其深度等于左右子树的最大深度加一。
&spm=1001.2101.3001.5002&articleId=7828895&d=1&t=3&u=e1155dc928db4c43ad76030ea9c2a00f)
3517

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



