Python solution, inorder /preorder .

  • 0

    inorder traversal

    class Solution(object):
    def isValidBST(self, root ):
        def dfs(root,pre=[-float('inf'),True]):
            if root and dfs(root.left,pre) and pre[1]:
            return pre[1]
        return dfs(root)

    preorder traversal ,Inspired by others.

    class Solution(object):
    def isValidBST(self,root,lessMe = float('inf'),largerMe = float('-inf')):
        return not root or largerMe<root.val<lessMe \
                and self.isValidBST(root.left,root.val,largerMe) \
                and self.isValidBST(root.right,lessMe,root.val)

Log in to reply

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