mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #1834 from ZerenZhang2022/patch-4
Update 0098.验证二叉搜索树.md
This commit is contained in:
@ -392,7 +392,26 @@ class Solution:
|
||||
return is_left_valid and is_right_valid
|
||||
return __isValidBST(root)
|
||||
```
|
||||
|
||||
**递归** - 避免初始化最小值做法:
|
||||
```python
|
||||
class Solution:
|
||||
def isValidBST(self, root: TreeNode) -> bool:
|
||||
# 规律: BST的中序遍历节点数值是从小到大.
|
||||
pre = None
|
||||
def __isValidBST(root: TreeNode) -> bool:
|
||||
nonlocal pre
|
||||
|
||||
if not root:
|
||||
return True
|
||||
|
||||
is_left_valid = __isValidBST(root.left)
|
||||
if pre and pre.val>=root.val: return False
|
||||
pre = root
|
||||
is_right_valid = __isValidBST(root.right)
|
||||
|
||||
return is_left_valid and is_right_valid
|
||||
return __isValidBST(root)
|
||||
```
|
||||
```python
|
||||
迭代-中序遍历
|
||||
class Solution:
|
||||
|
Reference in New Issue
Block a user