-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1991.py
More file actions
35 lines (32 loc) · 740 Bytes
/
1991.py
File metadata and controls
35 lines (32 loc) · 740 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
import collections
import sys
sys.setrecursionlimit(100000)
n = int(input())
graph = collections.defaultdict(list)
for _ in range(n):
parent, left, right = map(str, sys.stdin.readline().split())
graph[parent].append(left)
graph[parent].append(right)
def inorder(v):
if len(graph[v])==0 or v =='.':
return
inorder(graph[v][0])
print(v,end='')
inorder(graph[v][1])
def preorder(v):
if len(graph[v])==0 or v =='.':
return
print(v, end='')
preorder(graph[v][0])
preorder(graph[v][1])
def outorder(v):
if len(graph[v])==0 or v =='.':
return
outorder(graph[v][0])
outorder(graph[v][1])
print(v, end='')
preorder('A')
print()
inorder('A')
print()
outorder('A')