title: Return root-to-leaf paths of a binary tree as a Python list
body: <p>If I use a string, I can get all the <code>root-to-leaf</code> paths in a binary tree. However, if I change my data structure to a list I am unable to do so. Any advise would be helpful.</p>
  <pre><code>class Node:
    def __init__(self, val):
      self.val = val
      self.left = None
      self.right = None
      
  def all_tree_paths_str(root, paths=[], path = ''):
      if root:
        path += str(root.val)
        
        if not root.left and not root.right:  # if reach a leaf
          paths.append(path)  # update paths  
        else:
          all_tree_paths_str(root.left, paths, path)
          all_tree_paths_str(root.right, paths, path)
      return paths
  
  def all_tree_paths_list(root, paths=[], path = []):
      if root:
        path.append(root.val)
        
        if not root.left and not root.right:  # if reach a leaf
          paths.append(path)  # update paths  
          path = []
        else:
          all_tree_paths_list(root.left, paths, path)
          all_tree_paths_list(root.right, paths, path)
      return paths  
  
  a = Node('a')
  b = Node('b')
  c = Node('c')
  d = Node('d')
  e = Node('e')
  f = Node('f')
  
  a.left = b
  a.right = c
  b.left = d
  b.right = e
  c.right = f
   
  print(all_tree_paths_str(a))
  print(all_tree_paths_list(a))
  </code></pre>
  <p>For the test case above:</p>
  <pre><code>     a
     /   \
    b     c
   / \     \
  d   e     f
  </code></pre>
  <p>I actually a nested list:</p>
  <pre><code>[['a', 'b', 'd'], ['a', 'b', 'e'], ['a', 'c', 'f']]
  </code></pre>
  <p>But my code (<code>all_tree_paths_list</code>) returns an output like:</p>
  <pre><code>[['a', 'b', 'd', 'e', 'c', 'f'], ['a', 'b', 'd', 'e', 'c', 'f'], ['a', 'b', 'd', 'e', 'c', 'f']]
  </code></pre>
  <p>The closest I can get it is using a string instead of a list (<code>all_tree_paths_str</code>):</p>
  <pre><code>['abd', 'abe', 'acf']
  </code></pre>
  <p>I cannot figure out why my recursion returns all the nodes in the list. As @Leif suggested, it shouldn't do that... but it does.</p>