-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy path[10]_1.py
39 lines (33 loc) · 969 Bytes
/
[10]_1.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
class Node:
def __init__(self, data, left_node, right_node):
self.data = data
self.left_node = left_node
self.right_node = right_node
def pre_order(node):
print(node.data, end='')
if node.left_node != '.':
pre_order(tree[node.left_node])
if node.right_node != '.':
pre_order(tree[node.right_node])
def in_order(node):
if node.left_node != '.':
in_order(tree[node.left_node])
print(node.data, end='')
if node.right_node != '.':
in_order(tree[node.right_node])
def post_order(node):
if node.left_node != '.':
post_order(tree[node.left_node])
if node.right_node != '.':
post_order(tree[node.right_node])
print(node.data, end='')
n = int(input())
tree = {}
for i in range(n):
data, left_node, right_node = input().split()
tree[data] = Node(data, left_node, right_node)
pre_order(tree['A'])
print()
in_order(tree['A'])
print()
post_order(tree['A'])