LeetCode - Medium - 98. Validate Binary Search Tree

Topic

  • Tree
  • Recursion
  • Depth-first Search

Description

https://leetcode.com/problems/validate-binary-search-tree/

Given the root of a binary tree, determine if it is a valid binary search tree (BST).

A valid BST is defined as follows:

  • The left subtree of a node contains only nodes with keys less than the node’s key.
  • The right subtree of a node contains only nodes with keys greater than the node’s key.
  • Both the left and right subtrees must also be binary search trees.

Example 1:

Input: root = [2,1,3]
Output: true

Example 2:

Input: root = [5,1,4,null,null,3,6]
Output: false
Explanation: The root node's value is 5 but its right child's value is 4.

Constraints:

  • The number of nodes in the tree is in the range [ 1 , 1 0 4 ] [1, 10^4] [1,104].
  • − 2 31 < = N o d e . v a l < = 231 − 1 -2^{31} <= Node.val <= 2{31} - 1 231<=Node.val<=2311

Analysis

方法一:我写的,中序遍历模式递归版。

方法二:我写的,中序遍历模式迭代版。

方法三:别人写的,前序遍历模式递归版。

Submission

import java.util.LinkedList;

import com.lun.util.BinaryTree.TreeNode;

public class ValidateBinarySearchTree {
	
	//方法一:我写的,中序遍历模式递归版
    public boolean isValidBST(TreeNode root) {
    	Integer[] prev = {null};
    	boolean[] result = {true};
    	isValidBST(root, prev, result);
    	return result[0];
    }
    
    private void isValidBST(TreeNode node, Integer[] prev, boolean[] result) {
    	if(node == null || !result[0]) return;
    	isValidBST(node.left, prev, result);
    	if(prev[0] != null && prev[0] >= node.val) {
    		result[0] = false;
    		return;
    	}
    	prev[0] = node.val;
    	isValidBST(node.right, prev, result);
    }
    
    //方法二:我写的,中序遍历模式迭代版
    public boolean isValidBST2(TreeNode root) {
    	LinkedList<TreeNode> stack = new LinkedList<>();
    	TreeNode p = root;
    	Integer prev = null;
    	while(!stack.isEmpty() || p != null) {
    		if(p != null) {
    			stack.push(p);
    			p = p.left;
    		}else {
    			TreeNode node = stack.pop();
    			if(prev != null && prev >= node.val)
    				return false;
    			prev = node.val;
    			p = node.right;
    		}
    	}
    	return true;
    }
    
    //方法三:别人写的,前序遍历模式递归版
    public boolean isValidBST3(TreeNode root) {
        return isValidBST3(root, null, null);
    }

    private boolean isValidBST3(TreeNode root, Integer min, Integer max) {
        if(root == null) return true;
        if(min != null && root.val <= min) return false;
        if(max != null && root.val >= max) return false;
        
        return isValidBST3(root.left, min, root.val) && isValidBST3(root.right, root.val, max);
    }
    
}

Test

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

import com.lun.util.BinaryTree;

public class ValidateBinarySearchTreeTest {

	@Test
	public void test() {
		ValidateBinarySearchTree obj = new ValidateBinarySearchTree();

		assertTrue(obj.isValidBST(BinaryTree.integers2BinaryTree(2,1,3)));
		assertFalse(obj.isValidBST(BinaryTree.integers2BinaryTree(5,1,4,null,null,3,6)));
		assertFalse(obj.isValidBST(BinaryTree.integers2BinaryTree(5,4,6,null,null,3,7)));
	}
	
	@Test
	public void test2() {
		ValidateBinarySearchTree obj = new ValidateBinarySearchTree();
		
		assertTrue(obj.isValidBST2(BinaryTree.integers2BinaryTree(2,1,3)));
		assertFalse(obj.isValidBST2(BinaryTree.integers2BinaryTree(5,1,4,null,null,3,6)));
		assertFalse(obj.isValidBST2(BinaryTree.integers2BinaryTree(5,4,6,null,null,3,7)));
	}
	
	@Test
	public void test3() {
		ValidateBinarySearchTree obj = new ValidateBinarySearchTree();
		
		assertTrue(obj.isValidBST2(BinaryTree.integers2BinaryTree(2,1,3)));
		assertFalse(obj.isValidBST2(BinaryTree.integers2BinaryTree(5,1,4,null,null,3,6)));
		assertFalse(obj.isValidBST2(BinaryTree.integers2BinaryTree(5,4,6,null,null,3,7)));
	}
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值