每日一题力扣98 验证二叉搜索树

只要进行中序遍历,结果得到的数组是升序的就可以了。sorted默认是升序

class Solution:
    def isValidBST(self, root: TreeNode) -> bool:
        inorder=self.inorder(root)
        return inorder==list(sorted(set(inorder)))

    def inorder(self,root):
        if root is None:
            return []
        return self.inorder(root.left)+[root.val]+self.inorder(root.right)
原文地址:https://www.cnblogs.com/liuxiangyan/p/14661356.html