Clean python solution


  • 0
    M
    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def isValidBST(self, root):
            """
            :type root: TreeNode
            :rtype: bool
            """
            def dfs(node, f):
                if node == None:
                    return True
                ret = f(node.val)
                if node.left:
                    ret = ret and dfs(node.left, lambda x: f(x) and x < node.val)
                if node.right:
                    ret = ret and dfs(node.right, lambda x: f(x) and x > node.val)
                return ret
            return dfs(root, lambda x: True)
                    
    

Log in to reply
 

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