I'm practicing with recursion. I'm trying to write a function that will count all the nodes in a binary tree. given the following class definition.
1 2 3 4 5
class Binode
{public:
int n;
Binode* l, *r;
}
So what I've come with is
1 2 3 4 5 6 7 8 9
int number(Binode* p, int& x)
{ if (p == NULL)
return x;
else
{
n++;
return (number (p -> l, x)+ number (p -> r, x));
}
}
Is there anyway to intialise x at the beggining? Also can anyone think of way to do this without having x as a parameter? So just a Binode* as a paramater?
I' trying this one now which counts the depth of a tree. This is untested but I think it should work. Is their anyway you could do this with one parameter a Binode*?