I am trying to count every node of a binary tree internally (not passing in the root), all my attemps have ended in memory access violation or an inaccurate count (4 or 5) when it should be 10.
This is what I have tried
And yes I am NOT suppose to pass in the root.
1 2 3 4 5 6 7 8 9 10
int CountChildren()
{
if ( !Left() && !Right())
return 1;
if ( Left() )
return Left()->CountChildren() +1;
if ( Right() )
return Right()->CountChildren() +1;
}
I am writing your statement out as an if statement so I can understand it better, Would you mind converting it for me so I do not mess it up? I believe it reads like
I see, much cleaner using the ternary operators such as your initial solution. Thank you so much, this was giving me a headache, and in case you were wondering Im not lurking for HW answers, I am actually trying to learn this stuff better :)