If what you mean in CountNodes
is to count all non-leaf nodes you must change this line:
int count=1;
to read this:
int count = (root._left == null && root._right == null) ? 0 : 1;
(the opposite of what is in CountLeaves
).
And this will get you the height of the tree:
public int Height(TreeNode root)
{
int height = 1;
if (root._left != null)
height = Math.Max(height, Height(root._left));
if (root._right != null)
height = Math.Max(height, Height(root._right));
return height;
}
Martinho Fernandes
2009-01-02 18:16:13