forked from MTrajK/coding-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalid_bst.py
101 lines (83 loc) · 1.91 KB
/
valid_bst.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
'''
Valid binary search tree
Check if a given tree is a valid binary search tree.
Input: 5
/ \
1 7
/ \
6 8
Output: True
=========================================
Visit all nodes and check if the values are inside the boundaries.
When visiting the left child use the value of the parent node like an upper boundary.
When visiting the right child use the value of the parent node like a lower boundary.
Time Complexity: O(N)
Space Complexity: O(N) , because of the recursion stack (but this is the tree is one branch), O(LogN) if the tree is balanced.
'''
############
# Solution #
############
# import TreeNode class from tree_helpers.py
from tree_helpers import TreeNode
import math
def is_valid_bst(root):
return is_valid_sub_bst(root, -math.inf, math.inf)
def is_valid_sub_bst(node, lower, upper):
if node is None:
return True
if (node.val <= lower) or (node.val >= upper):
return False
# check left
if not is_valid_sub_bst(node.left, lower, node.val):
return False
# check right
if not is_valid_sub_bst(node.right, node.val, upper):
return False
return True
###########
# Testing #
###########
# Test 1
'''
5
/ \
1 4
/ \
3 6
'''
# Correct result => False
root = TreeNode(5, TreeNode(1), TreeNode(4, TreeNode(3), TreeNode(6)))
print(is_valid_bst(root))
# Test 2
'''
5
/ \
1 6
/ \
4 7
'''
# Correct result => False
root = TreeNode(5, TreeNode(1), TreeNode(6, TreeNode(4), TreeNode(7)))
print(is_valid_bst(root))
# Test 3
'''
5
/ \
1 6
/ \
7 8
'''
# Correct result => False
root = TreeNode(5, TreeNode(1), TreeNode(6, TreeNode(7), TreeNode(8)))
print(is_valid_bst(root))
# Test 4
'''
5
/ \
1 7
/ \
6 8
'''
# Correct result => True
root = TreeNode(5, TreeNode(1), TreeNode(7, TreeNode(6), TreeNode(8)))
print(is_valid_bst(root))