LeetCode - Medium - 114. Flatten Binary Tree to Linked List

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 {

	@Test
	public 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));
	}
	
	@Test
	public 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));
	}
	
	@Test
	public 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));
	}
	
	@Test
	public 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));
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值