-
Notifications
You must be signed in to change notification settings - Fork 0
/
is_bst.py
47 lines (33 loc) · 1006 Bytes
/
is_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
#!/usr/bin/python3
import sys, threading
sys.setrecursionlimit(10**7) # max depth of recursion
threading.stack_size(2**27) # new thread will get stack of such size
ROOT_NODE = 0
def is_bst(tree):
def visit(node):
if node == -1:
return
key, left_child, right_child = tree[node]
visit(left_child)
in_order.append(key)
visit(right_child)
def is_sorted(lst):
for i in range(1, len(lst)):
if lst[i - 1] > lst[i]:
return False
return True
if len(tree) < 2:
return True
in_order = []
visit(ROOT_NODE)
return is_sorted(in_order)
def main():
nodes = int(sys.stdin.readline().strip())
tree = []
for i in range(nodes):
tree.append(list(map(int, sys.stdin.readline().strip().split())))
if is_bst(tree):
print("CORRECT")
else:
print("INCORRECT")
threading.Thread(target=main).start()