简单求解二叉树的深度算法(二叉树以链表形式存储)
int getdepth(BTNode *p){
int LD,RD;
if(p == NULL){
return 0;
}else{
LD = getdepth(p->lchild);
RD = getdepth(p->rchild);
return (LD>RD?LD:RD)+1;
}
}
本文介绍了一种用于计算二叉树深度的递归算法。该算法通过遍历二叉树的左右子树并比较它们的深度来确定整棵树的最大深度。适用于以链表形式存储的二叉树。
简单求解二叉树的深度算法(二叉树以链表形式存储)
int getdepth(BTNode *p){
int LD,RD;
if(p == NULL){
return 0;
}else{
LD = getdepth(p->lchild);
RD = getdepth(p->rchild);
return (LD>RD?LD:RD)+1;
}
}
1213

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