I got bombed in a interview because I was asked to write an algorithm which would count the leaves of a tree, I did this:
class TreeNode{
TreeNode[] children;
int value;
ctor(){
children = new TreeNode[]();
}
}
class CountingClass{
int count;
ctor(){
count = 0;
}
void Count(TreeNode node)
{
if(node.Children.Count > 0)
{
foreach(TreeNode item in node.Children)
{
Count(item);
}
}
count++;
}
}
the interviewer appreciated demostrating my OOP approach but he wanted a solution contained in the function.
I got nervours and gave up after 20 minutes figuring it out. I was trying to pass an int as ref in the param but he said that is not what he was looking for either.
What is that thing I am missing here?
count
only when anode
has zero children. $\endgroup$