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: def traversal(self, root):
nonlocal candidate_list if root is None:
if not root:
return return
__traverse(root.left) self.traversal(root.left)
candidate_list.append(root.val) self.vec.append(root.val) # 将二叉搜索树转换为有序数组
__traverse(root.right) self.traversal(root.right)
def __is_sorted(nums: list) -> bool: def isValidBST(self, root):
for i in range(1, len(nums)): self.vec = [] # 清空数组
if nums[i] <= nums[i - 1]: # ⚠️ 注意: Leetcode定义二叉搜索树中不能有重复元素 self.traversal(root)
for i in range(1, len(self.vec)):
# 注意要小于等于,搜索树里不能有相同元素
if self.vec[i] <= self.vec[i - 1]:
return False return False
return True return True
__traverse(root)
res = __is_sorted(candidate_list)
return res
``` ```
**递归** - 标准做法 递归法(版本二)设定极小值,进行比较
```python ```python
class Solution: class Solution:
def isValidBST(self, root: TreeNode) -> bool: def __init__(self):
# 规律: BST的中序遍历节点数值是从小到大. self.maxVal = float('-inf') # 因为后台测试数据中有int最小值
cur_max = -float("INF")
def __isValidBST(root: TreeNode) -> bool:
nonlocal cur_max
if not root: def isValidBST(self, root):
if root is None:
return True return True
is_left_valid = __isValidBST(root.left) left = self.isValidBST(root.left)
if cur_max < root.val: # 中序遍历,验证遍历的元素是不是从小到大
cur_max = root.val if self.maxVal < root.val:
self.maxVal = root.val
else: else:
return False return False
is_right_valid = __isValidBST(root.right) right = self.isValidBST(root.right)
return left and right
return is_left_valid and is_right_valid
return __isValidBST(root)
``` ```
**递归** - 避免初始化最小值做法: 递归法(版本三)直接取该树的最小值
```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.pre = None # 用来记录前一个节点
pre = None
def __isValidBST(root: TreeNode) -> bool:
nonlocal pre
if not root: def isValidBST(self, root):
if root is None:
return True return True
is_left_valid = __isValidBST(root.left) left = self.isValidBST(root.left)
if pre and pre.val>=root.val: return False
pre = root if self.pre is not None and self.pre.val >= root.val:
is_right_valid = __isValidBST(root.right) return False
self.pre = root # 记录前一个节点
right = self.isValidBST(root.right)
return left and right
return is_left_valid and is_right_valid
return __isValidBST(root)
``` ```
迭代法
```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 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