Skip to content

Commit a932317

Browse files
authored
Update Validate Binary Search Tree - Leetcode 98.py
1 parent 4a9e4f7 commit a932317

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Validate Binary Search Tree - Leetcode 98/Validate Binary Search Tree - Leetcode 98.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,25 @@ def is_valid(node, minn, maxx):
1313

1414
# Time Complexity: O(n)
1515
# Space Complexity: O(h) { here "h" is height of tree }
16+
17+
18+
# Bootcamp solution
19+
class Solution:
20+
def isValidBST(self, root: Optional[TreeNode]) -> bool:
21+
stk = [(root, float('-inf'), float('inf'))]
22+
23+
while stk:
24+
node, minn, maxx = stk.pop()
25+
26+
if node.val <= minn or node.val >= maxx:
27+
return False
28+
else:
29+
if node.left:
30+
stk.append((node.left, minn, node.val))
31+
32+
if node.right:
33+
stk.append((node.right, node.val, maxx))
34+
35+
return True
36+
# Time Complexity: O(n)
37+
# Space Complexity: O(h) { here "h" is height of tree }

0 commit comments

Comments
 (0)