title: traversing through a tree
body: <p>I want to write a function which returns all the elements in a tree in a list. I'm not allowed to use global variables though. Here's what I tried:</p>
  
  <pre><code>def traverse(current node):    
      if no left child and no right child:
          return current nodes data
      else:
          if both left child and right child exist:
              return [current nodes data,left_child.traverse(),right_child._traverse()]
          elif no left child:   return [current node's data,right_child.traverse()]
          elif no right child:  return [current node's data,left_child.traverse()]
  </code></pre>
  
  <p>we used this example: (root is 2, left child is 1, right child is 3 and right child of right child is 4)</p>
  
  <pre><code>    2
  1      3
            4
  </code></pre>
  
  <p>calling traverse on this tree returned this:</p>
  
  <p><code>[2, 1, [3, 4]]</code></p>
  
  <p>So the only problem is that we can't get it all to fit within just one list. </p>
  
  <p>EDIT: Here are some of the node functions which can be called:
  <code>node.data</code>, <code>node.left</code>, <code>node.right</code></p>