-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_bst.py
19 lines (18 loc) · 958 Bytes
/
check_bst.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 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: Optional[TreeNode]) -> bool:
def valid(node, left, right):
if not node:
return True
if not (node.val > left and node.val < right):
return False
# For the left child, the right boundary is updated whereas the left
# boundary remains the same. The left boundary does not change as we travel one depth down.
return valid(node.left, left, node.val) and valid(node.right, node.val, right) # because of the and clause it will return false if any one is false
# because there is no upper or lower bound on the root, we do not assign them in the beginnign.
return valid(root, float(-inf), float(inf))