Skip to content

Commit e7ffca0

Browse files
committed
validating binary search tree solutions
1 parent 2d27f84 commit e7ffca0

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def check(self, root: Optional[TreeNode], minimum: int, maximum: int) -> bool:
9+
if root is None:
10+
return True
11+
12+
if root.val <= minimum or root.val >= maximum:
13+
return False
14+
15+
return self.check(root.left, minimum, root.val) and self.check(root.right, root.val, maximum)
16+
17+
def isValidBST(self, root: Optional[TreeNode]) -> bool:
18+
minimum_constraint = ((-2) ** 31) - 1
19+
maximum_constraint = 2 ** 31
20+
21+
return self.check(root, minimum_constraint, maximum_constraint)

0 commit comments

Comments
 (0)