设计一算法,计算给定二叉树T中度为2的结点个数.
收藏:
0
点赞数:
0
评论数:
0
1个回答

算法如下,将指向树的根节点的指针作为入参返回的即为度为2的全部结点的个数.

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);

}

点赞数:
0
评论数:
0
关注公众号
一起学习,一起涨知识