First of all, I swear this is not homework, it\'s a question I was asked in an interview. I think I made a mess of it (though I did realise the solution requires recursion).
This is a standard recursion problem:
count(): cnt = 1 // this node if (haveRight) cnt += right.count if (haveLeft) cnt += left.count return cnt;
Very inefficient, and a killer if the tree is very deep, but that's recursion for ya...