题目

解题思路

  1. 通过递归先序遍历树;
  2. 用List存储遍历后的结点;
  3. 遍历List重组链表。

代码展示

class Solution {
    private List<TreeNode> list = new ArrayList<>();
    public void flatten(TreeNode root) {
        if(root == null){
            return;
        }
        nextNode(root);
        root = list.get(0);
        TreeNode temp = root;
        for (int i = 1; i < list.size(); i++){
            temp.left = null;
            temp.right = list.get(i);
            temp = temp.right;
        }
        System.out.println(123);
    }
    public void nextNode(TreeNode root){
        if(root == null){
            return;
        }
        list.add(root);
        nextNode(root.left);
        nextNode(root.right);
    }
}
12-12 10:02