未验证 提交 ac372639 编写于 作者: C Chenjie Xu 提交者: GitHub

【98. 验证二叉搜索树】【Python】

上级 393c5c49
......@@ -309,3 +309,26 @@ void BST(TreeNode root, int target) {
<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
</p>
[ChenjieXu](https://github.com/ChenjieXu)提供第98题Python3代码:
```python
def isValidBST(self, root):
# 递归函数
def helper(node, lower = float('-inf'), upper = float('inf')):
if not node:
return True
val = node.val
if val <= lower or val >= upper:
return False
# 右节点
if not helper(node.right, val, upper):
return False
# 左节点
if not helper(node.left, lower, val):
return False
return True
return helper(root)
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册