前序遍历+重赋值
/**
* 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 {
List<Integer> list = new ArrayList<>();
public void flatten(TreeNode root) {
if(root == null) return ;
preOrder(root);
int i = 0;
while(i +1 < list.size()){
root.left = null;
root.right = new TreeNode(list.get(i+1));
root = root.right;
i++;
}
}
public void preOrder(TreeNode root){
if(root == null) return ;
list.add(root.val);
preOrder(root.left);
preOrder(root.right);
}
}