-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidateBinarySearchTree_Day64.py
More file actions
59 lines (47 loc) · 1.68 KB
/
Copy pathValidateBinarySearchTree_Day64.py
File metadata and controls
59 lines (47 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#Brute Approach
# Brute Force Approach
class Solution:
def isValidBST(self, root: Optional[TreeNode]) -> bool:
if not root:
return True
def findMax(node):
while node.right:
node = node.right
return node.val
def findMin(node):
while node.left:
node = node.left
return node.val
leftValid = (not root.left or findMax(root.left) < root.val) and self.isValidBST(root.left)
rightValid = (not root.right or findMin(root.right) > root.val) and self.isValidBST(root.right)
return leftValid and rightValid
#Better Approach
# Better Approach (Inorder Traversal with List)
class Solution:
def isValidBST(self, root: Optional[TreeNode]) -> bool:
inorder = []
def inorderTraversal(node):
if not node:
return
inorderTraversal(node.left)
inorder.append(node.val)
inorderTraversal(node.right)
inorderTraversal(root)
# Check if strictly increasing
for i in range(1, len(inorder)):
if inorder[i] <= inorder[i-1]:
return False
return True
#Optimal Apporach
# Optimal Approach (Range Checking)
class Solution:
def isValidBST(self, root: Optional[TreeNode]) -> bool:
def validate(node, low, high):
if not node:
return True
if not (low < node.val < high):
return False
return (validate(node.left, low, node.val) and
validate(node.right, node.val, high))
return validate(root, float("-inf"), float("inf"))
# Optimal: O(N) time, O(H) space.