Count knot with one successor in Tree

Hello!
Please someone to show me.
How to count knot with one successor in the tree?
Am i need a two count search function?
This is a function to count with two.


1
2
3
4
5
6
7
8
9
10
11
12
void search_counter(elem *t, int &counter)
{
	if (t != NULL)
	{
		if (t->right != NULL && t->left != NULL) 
counter++;
		search_counter(t->right, counter);
		search_counter(t->left, counter);
		
	}
}
Last edited on
You need to change the condition on line 5.

Right now it checks that both left and right are not NULL.

You need a condition where left or right is not NULL, but not both, and not neither.
That is, only one of left and right may be non-NULL.

Hint, the condition will be fairly long. It will look something like
 
if ( (a && b) || (c && d) )

where a, b, c, and d are something like t->left != NULL or t->left == NULL.


In English, the “knot” is called a node.
A node may have zero or more branches.
A node with zero branches is called a leaf.


Hope this helps.
Topic archived. No new replies allowed.