title: Use recursion to find longest path to leaf node inside binary tree with the three as a parameter
body: <p>I am self-studying for an exam and have run into a problem due to lack of examples &amp; answers. An exercise states the following:</p>
  
  <p>"Write an algorithm that uses recursion to calculate the height of a given binary tree, ie. the longest existing path from the root node to a leaf node."</p>
  
  <p>A primitive start for a method is given, and this method must be modified to form the final answer. No additional methods are allowed outside this method:</p>
  
  <pre><code>// Binary tree height, find the longest path to a leaf node
  public static int height(BTree t) {
      return 0;
  }
  </code></pre>
  
  <p>A node is added to the tree before each call of the function.
  The final given tree looks like this:</p>
  
  <pre><code>       10        
      __/  \__     
     5        15   
    / \      /  \  
   3   8   12    18
    \      /\      
     4    11 13
  </code></pre>
  
  <p>The following is what I have so far but it does not work properly:</p>
  
  <pre><code>// Binary tree height, find the longest path to a leaf node
  public static int height(BTree t) {
  
      BTreeNode n = t.getRoot();
  
      int height = 0;
      int calc = 0;
  
      if (n == null)
      {
          return height;
      }
      else
      {
          while (n!=null) {
              n = n.getLeftChild();
              calc++;
          }
      }
  
      if(calc &gt; height) {
          height = calc-1;
          height = 0;
      }
  
      n = t.getRoot();
      while (n!=null) {
          n = n.getRightChild();
          calc++;
      }
  
      if(calc &gt; height) {
          height = calc-1;
          height = 0;
      }
  
  
      return height;
  }  // height()
  </code></pre>
  
  <p>Please help me study for and pass the exam, all help is greatly appreciated!</p>