未验证 提交 41d0acc7 编写于 作者: B BruceCat 提交者: GitHub

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

【98. 验证二叉搜索树】【Python】
......@@ -310,7 +310,11 @@ void BST(TreeNode root, int target) {
<img src="../pictures/qrcode.jpg" width=200 >
</p>
======其他语言代码======
### c++
[dekunma](https://www.linkedin.com/in/dekun-ma-036a9b198/)提供第98题C++代码:
```C++
/**
......@@ -343,3 +347,30 @@ public:
}
};
```
### python
[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.
先完成此消息的编辑!
想要评论请 注册