leetcode第98题验证二叉搜索树--DFS

# 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

你可能感兴趣的:(深度优先,leetcode,算法)