mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Update 0098.验证二叉搜索树.md
增加comment
This commit is contained in:
@ -338,7 +338,7 @@ class Solution {
|
|||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
**递归** - 利用BST中序遍历特性
|
**递归** - 利用BST中序遍历特性,把树"压缩"成数组
|
||||||
```python
|
```python
|
||||||
# Definition for a binary tree node.
|
# Definition for a binary tree node.
|
||||||
# class TreeNode:
|
# class TreeNode:
|
||||||
@ -362,7 +362,7 @@ class Solution:
|
|||||||
|
|
||||||
def __is_sorted(nums: list) -> bool:
|
def __is_sorted(nums: list) -> bool:
|
||||||
for i in range(1, len(nums)):
|
for i in range(1, len(nums)):
|
||||||
if nums[i] <= nums[i - 1]:
|
if nums[i] <= nums[i - 1]: # ⚠️ 注意: Leetcode定义二叉搜索树中不能有重复元素
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -370,8 +370,11 @@ class Solution:
|
|||||||
res = __is_sorted(candidate_list)
|
res = __is_sorted(candidate_list)
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
```
|
||||||
|
|
||||||
# 简单递归法
|
**递归** -
|
||||||
|
|
||||||
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def isValidBST(self, root: TreeNode) -> bool:
|
def isValidBST(self, root: TreeNode) -> bool:
|
||||||
def isBST(root, min_val, max_val):
|
def isBST(root, min_val, max_val):
|
||||||
|
Reference in New Issue
Block a user