Topic
- Tree
- Depth-first Search
Description
https://leetcode.com/problems/flatten-binary-tree-to-linked-list/
Given the root of a binary tree, flatten the tree into a “linked list”:
The “linked list” should use the same TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null.
 The “linked list” should be in the same order as a pre-order traversal of the binary tree.
Example 1:

Input: root = [1,2,5,3,4,null,6]
Output: [1,null,2,null,3,null,4,null,5,null,6]
Example 2:
Input: root = []
Output: []
Example 3:
Input: root = [0]
Output: [0]
Constraints:
- The number of nodes in the tree is in the range [0, 2000].
- -100 <= Node.val <= 100
Follow up: Can you flatten the tree in-place (with O(1) extra space)?
Analysis
方法一:我写的,用到二叉树前序遍历模式的递归法。
方法二:别人写的,很简洁。
方法三:别人写的,跟方法一的核心思想类似,区别在于本方法只用单一方法就搞定。
方法四:别人写的,迭代法。
Submission
package com.lun.medium;import java.util.LinkedList;import com.lun.util.BinaryTree.TreeNode;public class FlattenBinaryTreeToLinkedList {//方法一:我写的,用到二叉树前序遍历模式的递归法public void flatten(TreeNode root) {flattenHelper(root);}private TreeNode flattenHelper(TreeNode node) {if(node == null)return null;TreeNode left = node.left; TreeNode right = node.right;node.left = null;node.right = flattenHelper(left);TreeNode p = node;while(p.right != null)p = p.right;p.right = flattenHelper(right);    	return node;}//方法二:别人写的,很简洁private TreeNode prev = null;public void flatten2(TreeNode root) {if (root == null)return;flatten2(root.right);flatten2(root.left);root.right = prev;root.left = null;prev = root;}public void setPrevNull() {this.prev = null;}//方法三:别人写的,跟方法一的核心思想类似,区别在于本方法只用单一方法就搞定public void flatten3(TreeNode root) {if (root == null) return;TreeNode left = root.left;TreeNode right = root.right;root.left = null;flatten(left);flatten(right);root.right = left;TreeNode cur = root;while (cur.right != null) cur = cur.right;cur.right = right;}//方法四:别人写的,迭代法public void flatten4(TreeNode root) {if (root == null) return;LinkedList<TreeNode> stk = new LinkedList<>();stk.push(root);while (!stk.isEmpty()){TreeNode curr = stk.pop();if (curr.right!=null)  stk.push(curr.right);if (curr.left!=null)  stk.push(curr.left);if (!stk.isEmpty()) curr.right = stk.peek();curr.left = null;  // dont forget this!! }}}Test
import static org.junit.Assert.*;
import org.junit.Test;import com.lun.util.BinaryTree;
import com.lun.util.BinaryTree.TreeNode;public class FlattenBinaryTreeToLinkedListTest {@Testpublic void test() {FlattenBinaryTreeToLinkedList obj = new FlattenBinaryTreeToLinkedList();TreeNode root1 = BinaryTree.integers2BinaryTree(1, 2, 5, 3, 4, null, 6);obj.flatten(root1);TreeNode expected1 = BinaryTree.integers2BinaryTree(1, null, 2, null, 3, null, 4, null, 5, null, 6);assertTrue(BinaryTree.equals(root1, expected1));TreeNode root2 = null;obj.flatten(root2);assertNull(root2);TreeNode root3 = BinaryTree.integers2BinaryTree(0);obj.flatten(root3);TreeNode expected3 = BinaryTree.integers2BinaryTree(0);assertTrue(BinaryTree.equals(root3, expected3));}@Testpublic void test2() {FlattenBinaryTreeToLinkedList obj = new FlattenBinaryTreeToLinkedList();TreeNode root1 = BinaryTree.integers2BinaryTree(1, 2, 5, 3, 4, null, 6);obj.flatten2(root1);TreeNode expected1 = BinaryTree.integers2BinaryTree(1, null, 2, null, 3, null, 4, null, 5, null, 6);assertTrue(BinaryTree.equals(root1, expected1));obj.setPrevNull();TreeNode root2 = null;obj.flatten2(root2);assertNull(root2);obj.setPrevNull();TreeNode root3 = BinaryTree.integers2BinaryTree(0);obj.flatten2(root3);TreeNode expected3 = BinaryTree.integers2BinaryTree(0);assertTrue(BinaryTree.equals(root3, expected3));}@Testpublic void test3() {FlattenBinaryTreeToLinkedList obj = new FlattenBinaryTreeToLinkedList();TreeNode root1 = BinaryTree.integers2BinaryTree(1, 2, 5, 3, 4, null, 6);obj.flatten3(root1);TreeNode expected1 = BinaryTree.integers2BinaryTree(1, null, 2, null, 3, null, 4, null, 5, null, 6);assertTrue(BinaryTree.equals(root1, expected1));TreeNode root2 = null;obj.flatten3(root2);assertNull(root2);TreeNode root3 = BinaryTree.integers2BinaryTree(0);obj.flatten3(root3);TreeNode expected3 = BinaryTree.integers2BinaryTree(0);assertTrue(BinaryTree.equals(root3, expected3));}@Testpublic void test4() {FlattenBinaryTreeToLinkedList obj = new FlattenBinaryTreeToLinkedList();TreeNode root1 = BinaryTree.integers2BinaryTree(1, 2, 5, 3, 4, null, 6);obj.flatten4(root1);TreeNode expected1 = BinaryTree.integers2BinaryTree(1, null, 2, null, 3, null, 4, null, 5, null, 6);assertTrue(BinaryTree.equals(root1, expected1));TreeNode root2 = null;obj.flatten4(root2);assertNull(root2);TreeNode root3 = BinaryTree.integers2BinaryTree(0);obj.flatten4(root3);TreeNode expected3 = BinaryTree.integers2BinaryTree(0);assertTrue(BinaryTree.equals(root3, expected3));}
}