single parent nodes

I'm trying to count the number of single parent nodes in this binary tree.
When I run my program it always returns 1. What am I doing wrong?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int binaryTreeType::singleParent(binaryTreeNode *p)
{   
	if(p==NULL)
		return 0;
	else
	{
			if (p->llink == NULL && p->rlink != NULL)
				return 1 + singleParent(p->rlink);
			else if(p->rlink == NULL && p->llink != NULL)
				return 1 + singleParent(p->llink);
			else
				return singleParent(p->llink);
}
}
I don't think you are handling all cases there. What if both children are null pointers? Then the else clause will fail. In the case where they are both non-null, you'll probably also want to sum the singeParent() results for each, rather than just the left side. Not sure if this will fix your problem though.
Thanks for the idea. I'll see what happens.
Topic archived. No new replies allowed.