-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_bst.py
25 lines (25 loc) · 911 Bytes
/
validate_bst.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def isValidBST(self, root: TreeNode) -> bool:
def check(root, lower, upper):
# case-1: Root is NULL
if root is None:
return True
# case-2: Value Exception
if root.val >= upper or root.val <= lower:
return False
status = True
# case-3: Subtree Test
if root.left is not None:
left = root.left
status = status & check(left, lower, root.val)
if root.right is not None:
right = root.right
status = status & check(right, root.val, upper)
return status
return check(root, float(-inf), float(inf))