判断一颗树是否为二叉查找树

题目原型:

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

Assume a 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.
基本思路:
本题我找到了比较笨的两种方法,一种是先求中序,然后比较;二是根据左子树的最大值小于根节点值,右子树最小值大于根节点的值;第三种比较简洁,是在网上找到的。
思路一:
网上代码太多,略。
思路二:
	public boolean isValidBST(TreeNode root)
	{
		if(root==null)
			return true;
		if(root.left==null&&root.right==null)
			return true;
		int left,right;
		
		if(root.left!=null)
			left = getMax(root.left);
		else
		    left = root.val-1;
		if(root.right!=null)
			right = getMin(root.right);
		else
		    right = root.val+1;
		if(left<root.val&&root.val<right)
			return isValidBST(root.left)&&isValidBST(root.right);
		else
			return false;
	}

	// 求一颗树中的最大值
	public int getMax(TreeNode root)
	{
		if (root.left == null && root.right == null)
			return root.val;
		if (root.left != null)
			if (root.left.left == null && root.left.right == null)
			{
				if (root.right == null)
					return root.val > root.left.val ? root.val : root.left.val;
				else if (root.right.left == null && root.right.right == null)
				{
					int tmp = root.left.val > root.right.val ? root.left.val
							: root.right.val;
					return root.val > tmp ? root.val : tmp;
				}
			}
		if (root.right != null)
			if (root.right.left == null && root.right.right == null)
			{
				if (root.left == null)
					return root.val > root.right.val ? root.val
							: root.right.val;
				else if (root.left.left == null && root.left.right == null)
				{
					int tmp = root.left.val > root.right.val ? root.left.val
							: root.right.val;
					return root.val > tmp ? root.val : tmp;
				}
			}
		int left = root.val, right = root.val;
		if (root.left != null)
			left = getMax(root.left);
		if (root.right != null)
			right = getMax(root.right);

		int tmp = left > right ? left : right;

		return root.val > tmp ? root.val : tmp;
	}
	//求每棵树的最小
	public int getMin(TreeNode root)
	{
		if (root.left == null && root.right == null)
			return root.val;
		if (root.left != null)
			if (root.left.left == null && root.left.right == null)
			{
				if (root.right == null)
					return root.val < root.left.val ? root.val : root.left.val;
				else if (root.right.left == null && root.right.right == null)
				{
					int tmp = root.left.val < root.right.val ? root.left.val
							: root.right.val;
					return root.val < tmp ? root.val : tmp;
				}
			}
		if (root.right != null)
			if (root.right.left == null && root.right.right == null)
			{
				if (root.left == null)
					return root.val < root.right.val ? root.val
							: root.right.val;
				else if (root.left.left == null && root.left.right == null)
				{
					int tmp = root.left.val < root.right.val ? root.left.val
							: root.right.val;
					return root.val < tmp ? root.val : tmp;
				}
			}
		int left = root.val, right = root.val;
		if (root.left != null)
			left = getMin(root.left);
		if (root.right != null)
			right = getMin(root.right);

		int tmp = left < right ? left : right;

		return root.val < tmp ? root.val : tmp;
	}


思路三:
	public  boolean isValidBST(TreeNode root) {
		return validate(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
	}
 
	public static boolean validate(TreeNode root, int min, int max) {
		if (root == null) {
			return true;
		}
 
		// not in range
		if (root.val <= min || root.val >= max) {
			return false;
		}
 
		// left subtree must be < root.val && right subtree must be > root.val
		return validate(root.left, min, root.val) && validate(root.right, root.val, max);
	}

个人比较喜欢思路三的解法!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值