[I've removed tabs and reformatted code...]
"Trent" <[EMAIL PROTECTED]> wrote:
> I got this section of code that I am trying to return
> the total number of nodes with only one child.
>
> template<class elemType>
> int binaryTreeType<elemType>::sCCount(nodeType<elemType> *p)
This design seems a little shaky to me, but...
> {
> if ( p == NULL )
> return 0;
> else if ( (p->llink == NULL && p->rlink != NULL)
> || (p->llink != NULL && p->rlink == NULL))
> return 1;
> else
> return sCCount(p->llink) + sCCount(p->rlink);
> }
>
> There are a total of 4 nodes with only 1 child, but the
> function is only returning 2.
>
> Any ideas?
You don't check how many more single child nodes there are
in the children of single child nodes.
return !p ? 0 : (!p->llink != !p->rlink)
+ sCCount(p->llink)
+ sCCount(p->rlink);
--
Peter