# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def isValidBST(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
def DFS(root_cur):
start=end=root_cur.val
if root_cur.left:
a=DFS(root_cur.left)
if not a or root_cur.val<=a[1]:
return False
else:
start=a[0]
if root_cur.right:
b=DFS(root_cur.right)
if not b or root_cur.val>=b[0]:
return False
else:
end=b[1]
return [start,end]
if DFS(root):return True
else:return False
|