@@ -0,0 +1,1728 @@
+// Given a binary tree, flatten it to a linked list in-place.%0A%0A// For example,%0A// Given%0A%0A//          1%0A//         / %5C%0A//        2   5%0A//       / %5C   %5C%0A//      3   4   6%0A// The flattened tree should look like:%0A//    1%0A//     %5C%0A//      2%0A//       %5C%0A//        3%0A//         %5C%0A//          4%0A//           %5C%0A//            5%0A//             %5C%0A//              6%0A%0A%0A/**%0A * Definition for binary tree%0A * public class TreeNode %7B%0A *     int val;%0A *     TreeNode left;%0A *     TreeNode right;%0A *     TreeNode(int x) %7B val = x; %7D%0A * %7D%0A */%0Apublic class Solution %7B%0A  // recursive approach, kind of in-place%0A    // public void flatten(TreeNode root) %7B%0A    //     if(root == null) return;%0A%0A    //     TreeNode left = root.left;%0A    //     TreeNode right = root.right;%0A%0A    //     if(left != null)%7B%0A    //       root.right = left;%0A    //       root.left = null;%0A    //       TreeNode rightMostOnLeftTree = left;%0A%0A    //       while(rightMostOnLeftTree.right != null)%7B%0A    //         rightMostOnLeftTree = rightMostOnLeftTree.right;%0A    //       %7D%0A%0A    //       rightMostOnLeftTree.right = right;%0A    //     %7D%0A%0A    //     flatten(root.right);%0A    // %7D%0A%0A  // in-order traversal approach. need extra list buffer.%0A    public void flatten(TreeNode root) %7B%0A        if(root == null) return;%0A%0A        List%3CTreeNode%3E list = new ArrayList%3CTreeNode%3E();%0A%0A        inOrder(list, root);%0A%0A        for(int i = 0; i %3C list.size() - 1; i++)%7B%0A            TreeNode current = list.get(i);%0A            current.left = null;%0A            current.right = list.get(i + 1);%0A        %7D%0A    %7D%0A%0A    public void inOrder(List%3CTreeNode%3E list, TreeNode node)%7B%0A      if(node == null) return;%0A%0A      list.add(node);%0A      inOrder(list, node.left);%0A      inOrder(list, node.right);%0A    %7D%0A%0A%7D%0A

add flatten binary tree to lined list solution