每日一题力扣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)

 

posted @ 2021-04-15 10:33  小千北同学超爱写代码  阅读(43)  评论(0编辑  收藏  举报