solution.md 3.0 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
# 验证二叉搜索树

<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://assets.leetcode.com/uploads/2020/12/01/tree1.jpg" 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://assets.leetcode.com/uploads/2020/12/01/tree2.jpg" 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

```