# 验证二叉搜索树

<div class="notranslate">
    <p>给你一个二叉树的根节点 <code>root</code> ，判断其是否是一个有效的二叉搜索树。</p>

    <p><strong>有效</strong> 二叉搜索树定义如下：</p>

    <ul>
        <li>节点的左子树只包含<strong> 小于 </strong>当前节点的数。</li>
        <li>节点的右子树只包含 <strong>大于</strong> 当前节点的数。</li>
        <li>所有左子树和右子树自身必须也是二叉搜索树。</li>
    </ul>

    <p>&nbsp;</p>

    <p><strong>示例 1：</strong></p>
    <img style="width: 302px; height: 182px;" src="https://img-service.csdnimg.cn/img_convert/3d85b39145ecc61874276bcb7cfe6765.jpeg" alt="">
    <pre><strong>输入：</strong>root = [2,1,3]
<strong>输出：</strong>true
</pre>

    <p><strong>示例 2：</strong></p>
    <img style="width: 422px; height: 292px;" src="https://img-service.csdnimg.cn/img_convert/f8c9b63a549daf28d19243f8141bb56d.jpeg" alt="">
    <pre><strong>输入：</strong>root = [5,1,4,null,null,3,6]
<strong>输出：</strong>false
<strong>解释：</strong>根节点的值是 5 ，但是右子节点的值是 4 。
</pre>

    <p>&nbsp;</p>

    <p><strong>提示：</strong></p>

    <ul>
        <li>树中节点数目范围在<code>[1, 10<sup>4</sup>]</code> 内</li>
        <li><code>-2<sup>31</sup> &lt;= Node.val &lt;= 2<sup>31</sup> - 1</code></li>
    </ul>
</div>

## template

```python
import sys
class TreeNode(object):
	def __init__(self, x):
		self.val = x
		self.left = None
		self.right = None
class List2Tree(object):
	def __init__(self, nums: list):
		self.nums = nums
		self.queue = []
		if len(nums) == 1:
			self.root = TreeNode(self.nums.pop(0))
		else:
			a = self.nums.pop(0)
			b = self.nums.pop(0)
			c = self.nums.pop(0)
			self.root = TreeNode(a)
			if b is not None:
				self.root.left = TreeNode(b)
			else:
				self.root.left = b
			if c is not None:
				self.root.right = TreeNode(c)
			else:
				self.root.right = c
			self.queue.append(self.root.left)
			self.queue.append(self.root.right)
	def convert(self):
		while len(self.nums) > 0 and len(self.queue)> 0:
			node = self.queue.pop(0)
			if node is not None:
				num= self.nums.pop(0)
				if num is not None:
					node.left = TreeNode(num)
				else:
					node.left = num
				if len(self.nums) > 0:
					num = self.nums.pop(0)
				else:
					num = None
				if num is not None:
					node.right = TreeNode(num)
				else:
					node.right = num
				self.queue.append(node.left)
				self.queue.append(node.right)
		return self.root
class Solution(object):
	def isValidBST(self, root):
		root = List2Tree(root).convert()
		return self.isVaild_helper(root, -sys.maxsize - 1, sys.maxsize)
	def isVaild_helper(self, root, minVal, maxVal):
		if root is None:
			return True
		if root.val >= maxVal or root.val <= minVal:
			return False
		return self.isVaild_helper(root.left, minVal, root.val) and self.isVaild_helper(root.right, root.val, maxVal)
# %%
s = Solution()
print(s.isValidBST([5,1,4,None,None,3,6]))
```

## 答案

```python

```

## 选项

### A

```python

```

### B

```python

```

### C

```python

```