---
# System prepended metadata

title: Validate Binary Search Tree
tags: [Tree, Easy]

---

# Validate Binary Search Tree
###### tags: `Easy`、`Tree`

![](https://i.imgur.com/E6hlB8z.png)

- 想法:
```python=
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isValidBST(self, root):
        stack, prev = [], float("-inf")

        while stack or root:
            while root:
                stack.append(root)
                root = root.left
            root = stack.pop()
            # If next element in inorder traversal
            # is smaller than the previous one
            # that's not BST.
            if root.val <= prev:
                return False
            prev = root.val
            root = root.right

        return True
```