Update 0098.验证二叉搜索树.md

This commit is contained in:
jianghongcheng
2023-05-23 20:47:45 -05:00
committed by GitHub
parent 7a544d95b8
commit 3058654e93

View File

@ -341,117 +341,113 @@ class Solution {
## Python ## Python
**递归** - 利用BST中序遍历特性,把树"压缩"成数组 递归法(版本一)利用中序递增性质,转换成数组
```python ```python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution: class Solution:
def isValidBST(self, root: TreeNode) -> bool: def __init__(self):
# 思路: 利用BST中序遍历的特性. self.vec = []
# 中序遍历输出的二叉搜索树节点的数值是有序序列
candidate_list = []
def __traverse(root: TreeNode) -> None:
nonlocal candidate_list
if not root:
return
__traverse(root.left)
candidate_list.append(root.val)
__traverse(root.right)
def __is_sorted(nums: list) -> bool:
for i in range(1, len(nums)):
if nums[i] <= nums[i - 1]: # ⚠️ 注意: Leetcode定义二叉搜索树中不能有重复元素
return False
return True
__traverse(root)
res = __is_sorted(candidate_list)
return res
```
**递归** - 标准做法 def traversal(self, root):
if root is None:
return
self.traversal(root.left)
self.vec.append(root.val) # 将二叉搜索树转换为有序数组
self.traversal(root.right)
```python def isValidBST(self, root):
class Solution: self.vec = [] # 清空数组
def isValidBST(self, root: TreeNode) -> bool: self.traversal(root)
# 规律: BST的中序遍历节点数值是从小到大. for i in range(1, len(self.vec)):
cur_max = -float("INF") # 注意要小于等于,搜索树里不能有相同元素
def __isValidBST(root: TreeNode) -> bool: if self.vec[i] <= self.vec[i - 1]:
nonlocal cur_max
if not root:
return True
is_left_valid = __isValidBST(root.left)
if cur_max < root.val:
cur_max = root.val
else:
return False return False
is_right_valid = __isValidBST(root.right) return True
return is_left_valid and is_right_valid
return __isValidBST(root)
``` ```
**递归** - 避免初始化最小值做法:
递归法(版本二)设定极小值,进行比较
```python ```python
class Solution: class Solution:
def isValidBST(self, root: TreeNode) -> bool: def __init__(self):
# 规律: BST的中序遍历节点数值是从小到大. self.maxVal = float('-inf') # 因为后台测试数据中有int最小值
pre = None
def __isValidBST(root: TreeNode) -> bool: def isValidBST(self, root):
nonlocal pre if root is None:
return True
if not root:
return True left = self.isValidBST(root.left)
# 中序遍历,验证遍历的元素是不是从小到大
is_left_valid = __isValidBST(root.left) if self.maxVal < root.val:
if pre and pre.val>=root.val: return False self.maxVal = root.val
pre = root else:
is_right_valid = __isValidBST(root.right) return False
right = self.isValidBST(root.right)
return is_left_valid and is_right_valid
return __isValidBST(root) return left and right
``` ```
递归法(版本三)直接取该树的最小值
```python ```python
迭代-中序遍历 # Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution: class Solution:
def isValidBST(self, root: TreeNode) -> bool: def __init__(self):
self.pre = None # 用来记录前一个节点
def isValidBST(self, root):
if root is None:
return True
left = self.isValidBST(root.left)
if self.pre is not None and self.pre.val >= root.val:
return False
self.pre = root # 记录前一个节点
right = self.isValidBST(root.right)
return left and right
```
迭代法
```python
# Definition for a binary tree node.
# class TreeNode:
# 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 = [] stack = []
cur = root cur = root
pre = None pre = None # 记录前一个节点
while cur or stack: while cur is not None or len(stack) > 0:
if cur: # 指针来访问节点,访问到最底层 if cur is not None:
stack.append(cur) stack.append(cur)
cur = cur.left cur = cur.left # 左
else: # 逐一处理节点 else:
cur = stack.pop() cur = stack.pop() # 中
if pre and cur.val <= pre.val: # 比较当前节点和前节点的值的大小 if pre is not None and cur.val <= pre.val:
return False return False
pre = cur pre = cur # 保存前一个访问的结点
cur = cur.right cur = cur.right # 右
return True return True
``` ```
```python
# 遵循Carl的写法只添加了节点判断的部分
class Solution:
def isValidBST(self, root: TreeNode) -> bool:
# method 2
que, pre = [], None
while root or que:
while root:
que.append(root)
root = root.left
root = que.pop()
# 对第一个节点只做记录,对后面的节点进行比较
if pre is None:
pre = root.val
else:
if pre >= root.val: return False
pre = root.val
root = root.right
return True
```
## Go ## Go