How do I calculate the number of "only child"-nodes in a binary tree?
<p>NOTICE: this is homework-related, but I'm not tagging it as such because the 'homework' tag is marked as obselete (?)</p>

<p>Using the following class that implements a binary tree...</p>

<pre><code>class TreeNode
{
  private Object value; 
  private TreeNode left, right;

   public TreeNode(Object initValue)
  { 
     value = initValue; 
     left = null; 
     right = null; 
  }

   public TreeNode(Object initValue, TreeNode initLeft, TreeNode initRight)
  { 
     value = initValue; 
     left = initLeft; 
     right = initRight; 
  }

   public Object getValue()
  { 
     return value; 
  }

   public TreeNode getLeft() 
  { 
     return left; 
  }

   public TreeNode getRight() 
  { 
     return right; 
  }

   public void setValue(Object theNewValue) 
  { 
     value = theNewValue; 
  }

   public void setLeft(TreeNode theNewLeft) 
  { 
     left = theNewLeft;
  }

   public void setRight(TreeNode theNewRight)
  { 
     right = theNewRight;
  }
</code></pre>

<p>}</p>

<p>I need to calculate the number of nodes in the binary tree that are "only children," this being defined as a node that doesn't have another node stemming from its parent.</p>

<p>This is what I have so far:</p>

<pre><code>public static int countOnlys(TreeNode t)
  {
     if(t == null)
         return 0;
     if(isAnOnlyChild(t))
         return 1;
     return countOnlys(t.getLeft()) + countOnlys(t.getRight());
  }
</code></pre>

<p>I don't know how to implement the <code>boolean</code> method <code>isAnOnlyChild(TreeNode t)</code></p>

<p>Could someone please help me?</p>
