Easy Python Solution with Hash Function


  • 0
    class Solution(object):
        def isValidBST(self, root):
            """
            :type root: TreeNode
            :rtype: bool
            """
            if not root:
                return True
            res = []
            self.helper(root, res)
            return hash(tuple(res)) == hash(tuple(sorted(res))) and len(set(res)) == len(res)
            
        def helper(self, root, res):
            if root.left:
                self.helper(root.left, res)
            res.append(root.val)
            if root.right:
                self.helper(root.right, res)
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.