算法如下,将指向树的根节点的指针作为入参返回的即为度为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);
}