-
Notifications
You must be signed in to change notification settings - Fork 0
/
089.py
69 lines (49 loc) · 1.51 KB
/
089.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""
Problem:
Determine whether a tree is a valid binary search tree.
A binary search tree is a tree with two children, left and right, and satisfies the
constraint that the key in the left child must be less than or equal to the root and
the key in the right child must be greater than or equal to the root.
"""
from DataStructures.Tree import BinaryTree, BinarySearchTree, Node
def is_binary_search_tree_helper(node: Node) -> bool:
if node is None:
return True
if node.left is None and node.right is None:
return True
elif (node.left and node.left.val > node.val) or (
node.right and node.right.val < node.val
):
return False
return is_binary_search_tree_helper(node.left) and is_binary_search_tree_helper(
node.right
)
def is_binary_search_tree(tree: BinaryTree) -> bool:
return is_binary_search_tree_helper(tree.root)
if __name__ == "__main__":
tree1 = BinarySearchTree()
tree1.add(5)
tree1.add(9)
tree1.add(1)
tree1.add(4)
tree1.add(10)
tree1.add(3)
tree1.add(2)
tree1.add(10)
tree1.add(7)
print(is_binary_search_tree(tree1))
tree2 = BinaryTree()
tree2.root = Node(5)
tree2.root.left = Node(4)
tree2.root.right = Node(6)
print(is_binary_search_tree(tree2))
tree3 = BinaryTree()
tree3.root = Node(5)
tree3.root.left = Node(6)
tree3.root.right = Node(4)
print(is_binary_search_tree(tree3))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(log(n)) [recursion depth]
"""