@@ -0,0 +1,1017 @@
+/**%0A * Definition for a binary tree node.%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 */%0Aimport java.util.List;%0Aimport java.util.ArrayList;%0A%0Aclass Solution %7B%0A    public void flatten(TreeNode root) %7B%0A        if (root == null) %7B%0A            return;%0A        %7D%0A        TreeNode dummy = new TreeNode(-1);%0A        dummy.right = root;%0A        List%3CTreeNode%3E list = new ArrayList%3C%3E();%0A        if (root.right != null) %7B%0A            list.add(root.right);%0A        %7D%0A        if (root.left != null) %7B%0A            list.add(root.left);%0A        %7D%0A        while (list.size() %3E 0) %7B%0A            TreeNode cur = list.remove(list.size()-1);%0A            root.left = null;%0A            root.right = cur;%0A            root = root.right;%0A            if (cur.right != null) %7B%0A                list.add(cur.right);%0A            %7D%0A            if (cur.left != null) %7B%0A                list.add(cur.left);%0A            %7D%0A        %7D%0A        root = dummy.right;%0A    %7D%0A%7D%0A

Flatten Binary Tree to Linked List