/* 先序遍历构建链表,重新构建树 */ LinkedList<Integer> list = new LinkedList<>(); public void flatten(TreeNode root) { preOrder(root); TreeNode res = root; list.poll(); while (!list.isEmpty()) { res.right = new TreeNode(list.poll()); res.left = null; res = res.right; } } public void preOrder(TreeNode root) { if (root==null) return; list.offer(root.val); preOrder(root.left); preOrder(root.right); }