-
Notifications
You must be signed in to change notification settings - Fork 0
/
1991.py
47 lines (33 loc) · 870 Bytes
/
1991.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
class Node:
def __init__(self, data, left, right):
self.data = data
self.left = left
self.right = right
def pre_order(node):
print(node.data, end='')
if node.left != '.':
pre_order(tree[node.left])
if node.right != '.':
pre_order(tree[node.right])
def in_order(node):
if node.left != '.':
in_order(tree[node.left])
print(node.data, end='')
if node.right != '.':
in_order(tree[node.right])
def post_order(node):
if node.left != '.':
post_order(tree[node.left])
if node.right != '.':
post_order(tree[node.right])
print(node.data, end='')
N = int(input())
tree = {}
for _ in range(N):
data, left, right = input().split(' ')
tree[data] = Node(data, left, right)
pre_order(tree['A'])
print()
in_order(tree['A'])
print()
post_order(tree['A'])