-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path11_binary_search_tree_validate.py
53 lines (42 loc) · 1.43 KB
/
11_binary_search_tree_validate.py
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
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class BinarySearchTree:
def __init__(self):
self.root = None
def isValidBST(self, root: 'TreeNode') -> bool:
def valid(node, left, right):
if not node:
return True
if not (left < node.val < right):
return False
return (valid(node.left, left, node.val) and
valid(node.right, node.val, right))
return valid(root, float("-inf"), float("+inf"))
# Helper function to build a BT from a list
def buildBT(nums):
if not nums:
return None
nodes = [TreeNode(val) if val is not None else None for val in nums]
for i in range(len(nums)):
if nodes[i]:
if 2 * i + 1 < len(nums):
nodes[i].left = nodes[2 * i + 1]
if 2 * i + 2 < len(nums):
nodes[i].right = nodes[2 * i + 2]
return nodes[0]
if __name__ == "__main__":
# Example 1
input1 = [2, 1, 3]
tree1 = buildBT(input1)
bst1 = BinarySearchTree()
bst1.root = tree1
print(bst1.isValidBST(bst1.root)) # Output: True
# Example 2
input2 = [5, 1, 4, None, None, 3, 6]
tree2 = buildBT(input2)
bst2 = BinarySearchTree()
bst2.root = tree2
print(bst2.isValidBST(bst2.root)) # Output: False