题目原型:
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);
}
个人比较喜欢思路三的解法!