mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Update 0098.验证二叉搜索树.md
补充:**递归** - 避免初始化最小值做法
This commit is contained in:
@ -392,7 +392,26 @@ class Solution:
|
|||||||
return is_left_valid and is_right_valid
|
return is_left_valid and is_right_valid
|
||||||
return __isValidBST(root)
|
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
|
```python
|
||||||
迭代-中序遍历
|
迭代-中序遍历
|
||||||
class Solution:
|
class Solution:
|
||||||
|
Reference in New Issue
Block a user