Skip to content

Commit 23142a7

Browse files
committed
validate binary search tree solution
1 parent 1d4ff54 commit 23142a7

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# TC: O(N)
2+
# SC: O(1)
3+
# Definition for a binary tree node.
4+
# class TreeNode:
5+
# def __init__(self, val=0, left=None, right=None):
6+
# self.val = val
7+
# self.left = left
8+
# self.right = right
9+
class Solution:
10+
11+
def isValid(self, left_bound, right_bound, root):
12+
if root is None:
13+
return True
14+
if left_bound is not None and left_bound >= root.val:
15+
return False
16+
if right_bound is not None and right_bound <= root.val:
17+
return False
18+
19+
return self.isValid(left_bound, root.val, root.left) and self.isValid(root.val, right_bound, root.right)
20+
21+
def isValidBST(self, root: Optional[TreeNode]) -> bool:
22+
return self.isValid(None, root.val, root.left) and self.isValid(root.val, None, root.right)
23+

0 commit comments

Comments
 (0)