Skip to content

Commit

Permalink
Merge pull request youngyangyang04#1834 from ZerenZhang2022/patch-4
Browse files Browse the repository at this point in the history
Update 0098.验证二叉搜索树.md
  • Loading branch information
youngyangyang04 authored Jan 5, 2023
2 parents 54174ea + b825b5a commit 2863a69
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion problems/0098.验证二叉搜索树.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 2863a69

Please sign in to comment.