- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryTree.py
More file actions
Latest commit
29 lines (19 loc) · 619 Bytes
/
Copy pathbinaryTree.py
File metadata and controls
29 lines (19 loc) · 619 Bytes
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
classTreeNode:
def__init__(self, key):
self.left=None
self.right=None
self.key=key
defis_bst(root):
result=False
min=float('-inf')
max=float('inf')
returnis_bst_recursive(root, min, max)
defis_bst_recursive(root, min, max):
result=False
result=root==Noneor (
isRootKeyValid(root, min, max) and \
is_bst_recursive(root.left, min, root.key-1) and \
is_bst_recursive(root.right, root.key+1, max))
returnresult
defisRootKeyValid(root, min, max):
returnroot.key>=minandroot.key<=max