title: Java Recursion - Alternative to passing-by-reference:
body: <p>I'm migrating from C to Java and I'm having difficulties with recursion, specially because in Java you can't pass an argument by reference.</p>
  
  <p>What I'm looking is not a solution/trick to force Java pass an argument by reference, but the recommended way to solve such a problem in Java.</p>
  
  <p>Let's take the recursive node insertion in a binary tree:</p>
  
  <pre><code>void nodeInsert(Node n, int a) {
      if (n == null)
          n = new Node(a);
  ...
  }
  </code></pre>
  
  <p>In C, by the end of the execution, the node <code>n</code> in the tree would point to the newly created node. In Java, however, <code>n</code> will still be <code>null</code> (because n is passed by value).</p>
  
  <p>What is the suggested Java approach for such problems?
  Some approaches I already tried:</p>
  
  <ul>
  <li>Using a static object to keep track of the parent (issue complicates when using generics).</li>
  <li>Passing the parent node as part of the function. It works but complicates the code a bit and doesn't look as a good solution.</li>
  <li>Creating an additional member pointing to the parent node but this is not a good solution, as it increases the space required by O(n);</li>
  </ul>
  
  <p>Any advice is welcome.</p>