title: Recursive Tree Traversal Method With Return Type Array
body: <p>Is there a way to recursively traverse a tree and return an array that is scoped to that recursive method?</p>
  
  <p>So I recently answered someone else's question on this topic. That question can be found here: <a href="https://stackoverflow.com/questions/16819177/traversing-a-tree-into-an-array">SO Question</a>. My solution uses an array outside of the scope of the recursion, and therefore the method cannot (or at least probably should not) return the array. However, is there a way to write a recursive method for traversing trees such that it returns an array? Even writing an initial method that calls the recursive one would be fine, but I can't think of a good way to do this.</p>
  
  <p>Here's the code that I suggested before:</p>
  
  <pre><code>private List nodeValues = new ArrayList();
  
  public void traversePreRecursive(BinarySearchTreeNode node) 
  {
      if (node != null)
      {
          nodeValues.add(node.getValue());
          traversePreRecursive(node.getLeft());
          traversePreRecursive(node.getRight());
      }
  }
  </code></pre>
  
  <p>As you can see the <code>ArrayList</code> is outside of the scope of the recursion - And therefore returning it doesn't make a lot of sense. Is there a better way to do this?</p>