class Solution:
def isValidBST(self, root: TreeNode) -> bool:
res = []
def helper(root):
if not root:
return
helper(root.left)
res.append(root.val)
helper(root.right)
helper(root)
return res == sorted(res) and len(set(res)) == len(res) # 必须加上后面这项判断,因为不能出现重复的值
leetcode刷题python之验证二叉搜索树
最新推荐文章于 2022-12-20 17:05:20 发布