Merge pull request #181 from jojoo15/patch-11

添加 0098.验证二叉搜索树 python版本
This commit is contained in:
Carl Sun
2021-05-19 09:45:14 +08:00
committed by GitHub

View File

@ -336,8 +336,26 @@ class Solution {
``` ```
Python Python
```python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
//递归法
class Solution:
def isValidBST(self, root: TreeNode) -> bool:
res = [] //把二叉搜索树按中序遍历写成list
def buildalist(root):
if not root: return
buildalist(root.left) //
res.append(root.val) //
buildalist(root.right) //
return res
buildalist(root)
return res == sorted(res) and len(set(res)) == len(res) //检查list里的数有没有重复元素以及是否按从小到大排列
```
Go Go
```Go ```Go
import "math" import "math"