function isValide(root, min, max) {
if (root === null) return true;
if (min != null && root.val <= min) return false;
if (max != null && root.val >= max) return false;
return isValide(root.left, min, root.val) && isValide(root.right, root.val, max);
}
var isValidBST = function(root) {
return isValide(root, -Infinity, Infinity);
};
var isValidBST = function(root) {
let stack = [];
let inorder = -Infinity;
while (stack.length || root !== null) {
while (root !== null) {
stack.push(root);
root = root.left;
}
root = stack.pop();
// 如果中序遍历得到的节点的值小于等于前一个 inorder,说明不是二叉搜索树
if (root.val <= inorder) {
return false;
}
inorder = root.val;
root = root.right;
}
return true;
};