title: Flattening a tree with parents/children and return all nodes
body: <p>It's probably too late, but I can't sleep until it's solved:</p>
  
  <p>I've got a tree with some parents, which have children, which have also children etc.</p>
  
  <p>Now I need a function to get all nodes from the tree.</p>
  
  <p>This is what currently works, but only with one level of depth:</p>
  
  <pre><code>def nodes_from_tree(tree, parent):
      r = []
      if len(tree.get_children(parent)) == 0:
          return parent
      for child in tree.get_children(parent):
          r.append(nodes_from_tree(tree, child))
      return r
  </code></pre>
  
  <p>Then I tried to pass <code>r</code> through, so it remembers the children, but I'm using the function more then once and <code>r</code> stores cumulatively all nodes, although I'm setting it to <code>r=[]</code>:</p>
  
  <pre><code>def nodes_from_tree(tree, parent, r=[]):
      r = []
      if len(tree.get_children(parent)) == 0:
          return parent
      for child in tree.get_children(parent):
          r.append(nodes_from_tree(tree, child, r))
      return r
  </code></pre>
  
  <p><strong>Edit</strong>: This is the tree structure:</p>
  
  <pre><code>parent1    parent2    parent3
     |          |          |
     |          |          |
   child        |          |
                |          |
        +--------------+   |
        |       |      |   |
      child   child  child |
        |                  |
    +---+---+              |
  child   child        +---+---+
                       |       |
                     child     |
                               |
                         +-----+-----+-----+
                         |     |     |     |
                       child child child child
  </code></pre>
  
  <p>Available methods:</p>
  
  <pre><code>tree.get_parents()       # returns the nodes of the very top level
  tree.get_children(node)  # returns the children of parent or child
  </code></pre>