98 Valid Binary Tree

二叉树遍历,回溯法,循环判断根节点和子节点。

class Solution(object):
    def isValidBST(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        def isValid(root,min,max):
            if root is None:
                return True
            if root.val<max and root.val>min:
                return isValid(root.left,min,root.val) and isValid(root.right,root.val,max)
            else:
                return False
        return isValid(root,-float('inf'), float('inf'))

results matching ""

    No results matching ""