编写递归算法,统计二叉树中度为2的结点个数
收起
知与谁同
2018-07-15 14:14:32
4096
0
2
条回答
写回答
取消
提交回答
-
int TwoBranch(Bitree T)
{
int s;
if(T == NULL)
return 0;
s = (T->lchild != NULL) && (T->rchild != NULL);
return s + TwoBranch(T->lchild) + TwoBranch(T->rchild);
}
2019-07-17 22:54:30
-
有几种程序可以考虑一下:
【1】
int TwoBranch(Bitree T)
{
int s;
if(T == NULL)
return 0;
s = (T->lchild != NULL) && (T->rchild != NULL);
return s + TwoBranch(T->lchild) + TwoBranch(T->rchild);
}
【2】
int leafnum(Bnode *t)
{
int i,j;
if(t==NULL)return 0;
else if(t->lchild==NULL && t->rchild==NULL)
return 1;
else
{
i = leafnum(t->lchild);
j=leafnum(t->rchild);
return (i+j);
}
}
【3】
int countDegreeTwo(TreeNode *root)
{
if (root == NULL)
return 0;
if (root->left != NULL && root->right != NULL)
return 1 + countDegreeTwo(root->left) + countDegreeTwo(root->right);
return countDegreeTwo(root->left) + countDegreeTwo(root->right);
}
2019-07-17 22:54:30