title: Binary tree get all nodes for every level
body: <p>I am trying to solve the following, say I have this binary tree...</p>
  
  <pre><code>       3
        / \
       9  20
         /  \
        15   7
  </code></pre>
  
  <p>Then I need to get all nodes at every level, so my result will be...</p>
  
  <pre><code>[[3],[9,20],[15,7]]
  </code></pre>
  
  <p>I believe I am getting close to a solution, but I'm not sure where I'm going wrong, if someone can help me with my solution that would be great, if I am on the wrong track, please let me know.</p>
  
  <p>My first step was to get the depth of the tree using the following function...</p>
  
  <pre><code>def get_depth(self, root):
      if root.left == None and root.right == None:
          return 1
      return max(self.get_depth(root.left), self.get_depth(root.right)) + 1
  </code></pre>
  
  <p>The depth is <code>3</code>.</p>
  
  <p>Next I called a function that was intended to give me the expected result...</p>
  
  <pre><code>def levelOrder(self, root):
      depth = self.get_depth(root)
      return self.find_comb(root, [[]]*depth, 0)
  
  def find_comb(self, root, result, level):
      if root is None:
          return None
      self.find_comb(root.left, result, level+1)
      self.find_comb(root.right, result, level+1)
      result[level].append(root.val)
      return result
  </code></pre>
  
  <p>My thought process was, I would recurse through the tree and the <code>level</code> parameter would keep track of the current level that I am on. Then I would append all <code>root.val</code> on that level to the result at that index. </p>
  
  <p>So let's say I am on level <code>1</code> (we start at 0), then the nodes at level <code>1</code> are <code>9</code> and <code>20</code>. So the result would look like <code>[[], [9, 20], [], []]</code>, and it would do this for every level of the tree. I hope my logic is clear. Instead the result I am getting is...</p>
  
  <pre><code>[[9, 15, 7, 20, 3], [9, 15, 7, 20, 3], [9, 15, 7, 20, 3]]
  </code></pre>