title: Counting nodes in binary tree
body: <p>I am having trouble counting the nodes in my binary tree. It is a real simple tree to start as illustrated in the diagram below. </p>
  
  <pre><code>                                (5)
                         (3)-------^-------(7)
                    (2)---^---(6)           ^-------(9)
                         (5)---^---(8)
  </code></pre>
  
  <p>I added eight nodes so there should be 8. However when I run my code it counts 7 nodes. I think it is just counting all the left nodes and the right nodes and not counting the root but  I set the number of nodes to 1 to count the root before it counts the left and right nodes. See the code below</p>
  
  <pre><code>private int getNumNodes(Node&lt;E&gt; root){
          numNodes = 1; // starts by counting the root
  
          // counts the left nodes
          if(root.left != null){
              numNodes += getNumNodes(root.getLeft());
          }
  
          // counts the right nodes
          if(root.right != null){
              numNodes += getNumNodes(root.getRight());
          }               
      return numNodes;
  }
  
  public int getNumNodes(){
      return root == null ? 0 : getNumNodes(root);
  }
  </code></pre>
  
  <p>It must be missing a count somewhere but I am not sure where it is happening. Could you guys please help me out. Thanks.</p>