Skip to content

Commit 86c5f80

Browse files
committed
adding isValidBST
1 parent ac0da2e commit 86c5f80

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 isValidBST(self, root: Optional[TreeNode]) -> bool:
9+
10+
def checkBST(root, min_val, max_val):
11+
if not root:
12+
return True
13+
14+
if not (min_val < root.val < max_val):
15+
return False
16+
return checkBST(root.left, min_val, root.val) and checkBST(root.right, root.val, max_val)
17+
18+
return checkBST(root, float('-inf'), float('inf'))

0 commit comments

Comments
 (0)