-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy path4.5-Validate_BST.py
44 lines (33 loc) · 1.22 KB
/
4.5-Validate_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
# CTCI 4.5
# Validate BST
class Node():
def __init__(self, value, left=None, right=None):
self.value, self.left, self.right = value, left, right
# My Solution
def validate(root):
return helper(root, None, None)
def helper(root, min, max):
if root is None:
return True
# In order left <= curr < right
if (min is not None and root.value <= min) or (max is not None and root.value > max):
return False
# If the left child is > the parent return false
if not helper(root.left, min, root.value):
return False
# If the right child is <= the parent return false
if not helper(root.right, root.value, max):
return False
return True
#-------------------------------------------------------------------------------
#Testing
import unittest
class Test(unittest.TestCase):
def test_validate_tree(self):
self.assertEqual(validate(Node(3,Node(1),Node(8))), True)
tree1 = Node(5,Node(3,Node(1),Node(4)),Node(7,Node(6),Node(8,None,Node(9))))
self.assertEqual(validate(tree1), True)
tree2 = Node(7,Node(3,Node(1),Node(8)),Node(9,Node(8),Node(11)))
self.assertEqual(validate(tree2), False)
if __name__ == "__main__":
unittest.main()