Skip to content

Instantly share code, notes, and snippets.

@alphaCoder
Created December 21, 2019 02:23
Show Gist options
  • Save alphaCoder/aa3fbe7d35edb340f8ceb1d9287a8b3f to your computer and use it in GitHub Desktop.
Save alphaCoder/aa3fbe7d35edb340f8ceb1d9287a8b3f to your computer and use it in GitHub Desktop.
Validate Binary Search Tree
# 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