Created
December 21, 2019 02:23
-
-
Save alphaCoder/aa3fbe7d35edb340f8ceb1d9287a8b3f to your computer and use it in GitHub Desktop.
Validate Binary Search Tree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Definition for a binary tree node. | |
# class TreeNode: | |
# def __init__(self, x): | |
# self.val = x | |
# self.left = None | |
# self.right = None | |
class Solution: | |
def isValidBST(self, root: TreeNode) -> bool: | |
def helper(tree, mi, mx): | |
if not tree: | |
return True | |
if not mi < tree.val < mx: | |
return False | |
return helper(tree.left, mi, tree.val) and helper(tree.right, tree.val, mx) | |
return helper(root, float('-inf'), float('inf')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment