如何找到只剩下孩子的二叉树中的节点数?
LeftNode(root)
{
if(root==NULL)
return 0;
if(root->left!=null && root-right==null)
return (1+LeftNode(root->left));
return (LeftNode (root->left) + LeftNode (root->right))
}
最佳答案 我会这样做(C):
int leftNode(Node * root)
{
if (root == nullptr)
return 0;
// c is 1 if root has a left child and has not a right one
int c = root->left != nullptr and root->right == nullptr ? 1 : 0;
return c + leftNode(root->left) + leftNode(root->right);
}