-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathis_bst.py
More file actions
42 lines (32 loc) · 763 Bytes
/
Copy pathis_bst.py
File metadata and controls
42 lines (32 loc) · 763 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class BinaryTreeNode:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
# inorder traversal should always increase
bst = BinaryTreeNode(5)
bst.left = BinaryTreeNode(4)
bst.right = BinaryTreeNode(6)
bst.left.left = BinaryTreeNode(100)
def is_bst(root):
"""
Args:
root(BinaryTreeNode_int32)
Returns:
bool
"""
values = []
result = True
def dfs(node):
if not node:
return None
dfs(node.left)
values.append(node.value)
dfs(node.right)
dfs(root)
for i in range(len(values) - 1):
if values[i] >= values[i + 1]:
result = False
# Write your code here.
return result
print(is_bst(bst))