Validate Binary Search Tree
An example:
2
/ \
1 4
/ \
3 5
The above binary tree is serialized as {2,1,4,#,#,3,5} (in level order). An example:
2
/ \
1 4
/ \
3 5
The above binary tree is serialized as {2,1,4,#,#,3,5} (in level order). public class Solution {
/**
* @param root: The root of binary tree.
* @return: True if the binary tree is BST, or false
*/
// 使用分治法
private class Return {
boolean isValid = true;
long min = Integer.MAX_VALUE;
long max = Integer.MIN_VALUE;
public Return(boolean b, long min, long max) {
isValid = b;
this.min = min;
this.max = max;
}
}
public boolean isValidBST(TreeNode root) {
if (root == null) return true;
Return ret = helper(root);
return ret.isValid;
}
private Return helper(TreeNode root) {
if (root == null) return new Return(true, (long)Integer.MAX_VALUE + 1, (long)Integer.MIN_VALUE - 1);
if (root.left == null && root.right == null) {
return new Return(true, root.val, root.val);
}
Return left = helper(root.left);
Return right = helper(root.right);
boolean flag = true;
long max = Math.max(left.max, Math.max(right.max, root.val));
long min = Math.min(left.min, Math.min(right.min, root.val));
if (! left.isValid || ! right.isValid || root.val >= right.min || root.val <= left.max) {
flag = false;
}
return new Return(flag, min, max);
}
} 1 1
/ \ --------- \
1 1 1
\
1
有相同的 inorderList: [1,1,1] class Solution(object):
def isValidBST(self, root):
# 返回(min, max),如果检查出已经invalid,返回None
def helper(root):
if not root:
return (float('inf'), float('-inf'))
left = helper(root.left)
right = helper(root.right)
if not left or not right:
return None
if left[1] >= root.val or right[0] <= root.val:
return None
return (min(left[0], right[0], root.val), max(left[1], right[1], root.val))
return helper(root) is not None public class Solution {
/*
* @param root: The root of binary tree.
* @return: True if the binary tree is BST, or false
*/
public boolean isValidBST(TreeNode root) {
if (root == null) return true;
if (! isValidBST(root.left) || ! isValidBST(root.right)) {
return false;
}
long leftMax;
long rightMin;
TreeNode node = root.left;
while (node != null && node.right != null) node = node.right;
leftMax = node != null ? node.val : (long)Integer.MIN_VALUE - 1;
node = root.right;
while (node != null && node.left != null) node = node.left;
rightMin = node != null ? node.val : (long)Integer.MAX_VALUE + 1;
if (root.val > leftMax && root.val < rightMin) return true;
else return false;
}
} # inorder traversal, check if is increasing
class Solution:
def isValidBST(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if not root: return True
stack = []
while root:
stack.append(root)
root = root.left
prev = float('-inf') # 跟踪前一个元素,初始化为负无穷
while stack:
node = stack.pop()
if node.val <= prev: # 说明不是递增,则不是BST
return False
prev = node.val # 更新prev
node = node.right
while node:
stack.append(node)
node = node.left
return Trueclass Solution {
public boolean isValidBST(TreeNode root) {
return helper(root, (long)Integer.MIN_VALUE - 1, (long)Integer.MAX_VALUE + 1);
}
// 每次检查当前root.val是否在 alpha和beta之间,alpha为lower bound, beta 为 upper bound
private boolean helper(TreeNode root, long alpha, long beta) {
if (root == null) return true;
if (root.val <= alpha || root.val >= beta) return false;
return helper(root.left, alpha, root.val) && helper(root.right, root.val, beta);
}
}