```
# 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)
```