title: Flatten Binary Tree to Linked List
body: <p>Given a binary tree, flatten it to a linked list in-place.</p>
  
  <p>For example, given the following tree:</p>
  
  <pre><code>
      1
     / \
    2   5
   / \   \
  3   4   6
  </code></pre>
  
  <p>The flattened tree should look like:</p>
  
  <pre><code>1
   \
    2
     \
      3
       \
        4
         \
          5
           \
            6
  </code></pre>
  
  <p>I'm of the other solutions out there but I want to ask why is it when I run my code, the output just matches the input tree.</p>
  
  <pre><code>public void flatten(TreeNode root) {
          if(root == null)
              return;
          TreeNode newRoot = new TreeNode(root.val);
          List&lt;TreeNode&gt; list = new ArrayList&lt;&gt;();
  
          helper(root,list);
          TreeNode current = newRoot;
          for(int i = 1; i &lt; list.size();i++) {
              current.right = new TreeNode(list.get(i).val);
              current = current.right;
          }
          root = newRoot;
      }
  
      public void helper(TreeNode oldNode,List&lt;TreeNode&gt; list) {
          if(oldNode != null) {
              list.add(oldNode);
              helper(oldNode.left,list);
              helper(oldNode.right,list);
          }
      }
  </code></pre>