代码:
0124
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val = val; }* TreeNode(int val, TreeNode left, TreeNode right) {* this.val = val;* this.left = left;* this.right = right;* }* }*/
class Solution {public void flatten(TreeNode root) {List<TreeNode> list = new ArrayList<TreeNode>();preorderTrversal(root,list);int size = list.size();for(int i=1;i<size;i++){TreeNode prev = list.get(i-1);TreeNode curr = list.get(i);prev.left = null;prev.right = curr;}}public void preorderTrversal(TreeNode root,List<TreeNode> list){if(root!=null){list.add(root);preorderTrversal(root.left,list);preorderTrversal(root.right,list);}}
}