title: sum of binary tree at a specific depth
body: <p>I am trying to recursively traverse a tree and return the sum of a given depth. Say I want the sum at depth = 2, it should recurse and subtract 1 until depth is 0, which then will add that node to the sum. It keeps summing the whole tree instead of just one level and I'm not sure where the mistake is. Also, I am supposed to use the interface ReduceFunction.</p>
  
  <pre><code>public interface ReduceFunction&lt;InT,OutT&gt; {
  
      public OutT combine(OutT soFar, InT x);     
  
      public OutT initialValue();
  }
  
  public T reduce(int n, ReduceFunction&lt;T,T&gt; f) {
          return rdceHelper(n, root, f, f.initialValue());               
  }
  
  private T rdceHelper(int depth, TreeNode&lt;T&gt; node, ReduceFunction&lt;T,T&gt; f, T sum){
      if(node == null || depth &lt; 0){
          return f.initialValue();
      }
  
      if(depth == 0){
          sum = f.combine(sum, node.data);
      }
  
      if(node.left != null){
          sum = f.combine(sum, rdceHelper(depth-1, node.left, f, sum));
      }
      if(node.right != null){
          sum = f.combine(sum, rdceHelper(depth-1, node.right, f, sum));
      }
  
      return sum;
  }
  </code></pre>