forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert.py
61 lines (46 loc) · 1.07 KB
/
insert.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
class node:
def __init__(self,val):
self.val = val
self.child = 1
self.l = None
self.r = None
def insert(root, t):
if root == None:
here = node(t)
return here
if t < root.val:
root.l = insert(root.l, t)
if t >= root.val:
root.r = insert(root.r, t)
root.child += 1
return root
def ways(root):
if root == None:
return 1
total = 1;
if root.r != None and root.l != None:
total = table[root.l.child][root.r.child]
total *= ways(root.l)
total *= ways(root.r)
return total
def printtree(root):
if root == None:
return
print(root.val)
printtree(root.l)
printtree(root.r)
table = [[1 for i in range(101)] for j in range(101)]
for i in range(1,101):
for j in range(1,101):
table[i][j] = table[i-1][j] + table[i][j-1];
import fileinput
while(True):
root = None
try:
n = int(input())
t = input().split()
except:
break
for i in t:
root = insert(root,int(i))
print(ways(root))