class Solution {
public void flatten(TreeNode root) {
List<TreeNode> list = new ArrayList<TreeNode>();
preorder(root, list);
for (int i = 1; i < list.size(); i++) {
TreeNode prev = list.get(i - 1);
TreeNode curr = list.get(i);
prev.left = null;
prev.right = curr;
}
}
public void preorder(TreeNode root, List<TreeNode> list) {
if (root == null) return;
list.add(root);
preorder(root.left, list);
preorder(root.right, list);
}
}