/* 先序遍历构建链表,重新构建树 */ 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); }
原文地址:https://www.cnblogs.com/stAr-1/p/8341045.html
时间: 2024-07-29 18:01:38