leetcode刷题python之验证二叉搜索树

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)