1. function isValide(root, min, max) {
    2. if (root === null) return true;
    3. if (min != null && root.val <= min) return false;
    4. if (max != null && root.val >= max) return false;
    5. return isValide(root.left, min, root.val) && isValide(root.right, root.val, max);
    6. }
    7. var isValidBST = function(root) {
    8. return isValide(root, -Infinity, Infinity);
    9. };
    1. var isValidBST = function(root) {
    2. let stack = [];
    3. let inorder = -Infinity;
    4. while (stack.length || root !== null) {
    5. while (root !== null) {
    6. stack.push(root);
    7. root = root.left;
    8. }
    9. root = stack.pop();
    10. // 如果中序遍历得到的节点的值小于等于前一个 inorder,说明不是二叉搜索树
    11. if (root.val <= inorder) {
    12. return false;
    13. }
    14. inorder = root.val;
    15. root = root.right;
    16. }
    17. return true;
    18. };