-
Notifications
You must be signed in to change notification settings - Fork 0
/
093.py
148 lines (115 loc) · 3.03 KB
/
093.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""
Problem:
Given a tree, find the largest tree/subtree that is a BST.
Given a tree, return the size of the largest tree/subtree that is a BST.
"""
from sys import maxsize
from typing import Tuple
from DataStructures.Tree import BinaryTree, Node
def get_largest_bst_size_helper(node: Node) -> Tuple[int, Node, bool, int, int]:
if not node:
return 0, node, True, maxsize, -maxsize
if not node.left and not node.right:
return 1, node, True, node.val, node.val
l_height, l_root, l_is_bst, l_max_val, l_min_val = get_largest_bst_size_helper(
node.left
)
r_height, r_root, r_is_bst, r_max_val, r_min_val = get_largest_bst_size_helper(
node.right
)
if l_is_bst and r_is_bst:
if node.left and node.right:
if l_max_val <= node.val <= r_min_val:
return (l_height + r_height + 1), node, True, r_max_val, l_min_val
else:
if node.left and node.val > l_max_val:
return l_height + 1, node, True, node.val, l_min_val
elif node.right and node.val < r_min_val:
return r_height + 1, node, True, r_max_val, node.val
if l_height > r_height:
return l_height, l_root, False, l_max_val, l_min_val
return r_height, r_root, False, r_max_val, r_min_val
def get_largest_bst_size(tree: BinaryTree) -> Tuple[int, int]:
size, node, _, _, _ = get_largest_bst_size_helper(tree.root)
return size, node.val
if __name__ == "__main__":
a = Node(3)
b = Node(2)
c = Node(6)
d = Node(1)
e = Node(1)
f = Node(4)
a.left = b
a.right = c
b.left = d
b.right = e
c.left = f
tree = BinaryTree()
tree.root = a
print(tree)
print("Size: {}\tNode Val: {}".format(*get_largest_bst_size(tree)))
a = Node(3)
b = Node(2)
c = Node(6)
d = Node(1)
e = Node(4)
f = Node(4)
a.left = b
a.right = c
b.left = d
b.right = e
c.left = f
tree = BinaryTree()
tree.root = a
print(tree)
print("Size: {}\tNode Val: {}".format(*get_largest_bst_size(tree)))
a = Node(1)
b = Node(2)
c = Node(6)
d = Node(1)
e = Node(3)
f = Node(4)
a.left = b
a.right = c
b.left = d
b.right = e
c.left = f
tree = BinaryTree()
tree.root = a
print(tree)
print("Size: {}\tNode Val: {}".format(*get_largest_bst_size(tree)))
a = Node(3)
b = Node(2)
c = Node(6)
d = Node(1)
e = Node(3)
f = Node(4)
a.left = b
a.right = c
b.left = d
b.right = e
c.left = f
tree = BinaryTree()
tree.root = a
print(tree)
print("Size: {}\tNode Val: {}".format(*get_largest_bst_size(tree)))
a = Node(3)
b = Node(1)
c = Node(6)
d = Node(0)
e = Node(2)
f = Node(4)
a.left = b
a.right = c
b.left = d
b.right = e
c.left = f
tree = BinaryTree()
tree.root = a
print(tree)
print("Size: {}\tNode Val: {}".format(*get_largest_bst_size(tree)))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(log(n))
"""