Skip to content

Commit af9a5cf

Browse files
committed
solve : validate binary search tree
1 parent d551167 commit af9a5cf

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# TC : O(n) where n being the number of nodes of the tree
2+
# SC : O(n) where n being the size of sum of every nodes
3+
class Solution:
4+
def isValidBST(self, root: Optional[TreeNode]) -> bool:
5+
def is_valid_bst(node, min_val, max_val):
6+
if not node:
7+
return True
8+
9+
if node.val <= min_val or node.val >= max_val:
10+
return False
11+
12+
return is_valid_bst(node.left, min_val, node.val) and is_valid_bst(
13+
node.right, node.val, max_val
14+
)
15+
16+
return is_valid_bst(root, float("-inf"), float("inf"))

0 commit comments

Comments
 (0)