LeetCode - Easy - 897. Increasing Order Search Tree

Topic

  • Tree
  • Depth-first Search
  • Recursion

Description

https://leetcode.com/problems/increasing-order-search-tree/

Given the root of a binary search tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only one right child.

Example 1:

Input: root = [5,3,6,2,4,null,8,1,null,null,null,7,9]
Output: [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]

Example 2:

Input: root = [5,1,7]
Output: [1,null,5,null,7]

Constraints:

  • The number of nodes in the given tree will be in the range [1, 100].
  • 0 <= Node.val <= 1000

Analysis

基本思想:result = inorder(root.left) + root + inorder(root.right)

方法一:我写的,递归法

方法二:别人写的,递归法

Submission

import com.lun.util.BinaryTree.TreeNode;

public class IncreasingOrderSearchTree {
	
	//方法一:我写的,递归法
    public TreeNode increasingBST(TreeNode root) {
		if(root == null) return null;
    	
    	TreeNode left = increasingBST(root.left);
    	
    	if(left != null) {
	    	TreeNode p = left;
	    	
	    	while(p.right != null) {
	    		p = p.right;
	    	}
	    	
	    	p.right = root;
    	}
    	
    	root.left = null;
    	root.right = increasingBST(root.right);
    	
    	return left == null ? root : left;
    }
    
    //方法二:别人写的,递归法
    public TreeNode increasingBST2(TreeNode root) {
        return increasingBST2(root, null);
    }

    private TreeNode increasingBST2(TreeNode root, TreeNode tail) {
        if (root == null) return tail;
        TreeNode res = increasingBST2(root.left, root);
        root.left = null;
        root.right = increasingBST2(root.right, tail);
        return res;
    }
    
}

Test

import static org.junit.Assert.*;
import org.junit.Test;

import com.lun.util.BinaryTree;
import com.lun.util.BinaryTree.TreeNode;

public class IncreasingOrderSearchTreeTest {

	@Test
	public void test() {
		IncreasingOrderSearchTree obj = new IncreasingOrderSearchTree();
		
		TreeNode root1 = BinaryTree.integers2BinaryTree(5, 3, 6, 2, 4, null, 8, 1, null, null, null, 7, 9);
		TreeNode expected1 = BinaryTree.integers2BinaryTree(1, null, 2, null, 3, null, 4, null, 5, null, 6, null, 7, null, 8, null, 9);
		assertTrue(BinaryTree.equals(obj.increasingBST(root1), expected1));
		
		TreeNode root2 = BinaryTree.integers2BinaryTree(5, 1, 7);
		TreeNode expected2 = BinaryTree.integers2BinaryTree(1,null,5,null,7);
		assertTrue(BinaryTree.equals(obj.increasingBST(root2), expected2));
	}
	
	@Test
	public void test2() {
		IncreasingOrderSearchTree obj = new IncreasingOrderSearchTree();
		
		TreeNode root1 = BinaryTree.integers2BinaryTree(5, 3, 6, 2, 4, null, 8, 1, null, null, null, 7, 9);
		TreeNode expected1 = BinaryTree.integers2BinaryTree(1, null, 2, null, 3, null, 4, null, 5, null, 6, null, 7, null, 8, null, 9);
		assertTrue(BinaryTree.equals(obj.increasingBST2(root1), expected1));
		
		TreeNode root2 = BinaryTree.integers2BinaryTree(5, 1, 7);
		TreeNode expected2 = BinaryTree.integers2BinaryTree(1,null,5,null,7);
		assertTrue(BinaryTree.equals(obj.increasingBST2(root2), expected2));
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值