树与二叉树
1.
//已知二叉树采用二叉链表存储结构,根节点所在链接点的地址为T,写一递归算法,求该二叉树中叶结点的数目
int COUNTLEAF(BTREE T)
{
if (T == NULL) return 0;
if (T->lchild == NULL && T->rchild == NULL) {
return 1;
return COUNTLEAF(T-<lchild) + COUNTLEAF(T->rchild);
}
int COUNTLEAF(BTREE T)
{
if (T == NULL) return 0;
if (T->lchild == NULL && T->rchild == NULL) {
return 1;
return COUNTLEAF(T-<lchild) + COUNTLEAF(T->rchild);
}
}
2.
// 已知二叉树采用二叉链表存储结构,根节点所在链接点的地址为T,写一递归算法,求该二叉树的深度
int BTDEPTH(BTREE T)
{
int leftDep, rightDep;
if (T == NULL) {
return 0;
}else{
leftDep = BTDEPTH(T->lchild);
rightDep = BTDEPTH(T->rchild);
if (leftDep>rightDep)
return leftDep + 1;
else
return rightDep + 1;
}
int BTDEPTH(BTREE T)
{
int leftDep, rightDep;
if (T == NULL) {
return 0;
}else{
leftDep = BTDEPTH(T->lchild);
rightDep = BTDEPTH(T->rchild);
if (leftDep>rightDep)
return leftDep + 1;
else
return rightDep + 1;
}
}
结束