-
Notifications
You must be signed in to change notification settings - Fork 0
/
1991.py
47 lines (33 loc) · 829 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
def preorder(node):
print(node.item, end = "")
if node.left != ".":
preorder(tree[node.left])
if node.right != ".":
preorder(tree[node.right])
def midorder(node):
if node.left != ".":
midorder(tree[node.left])
print(node.item, end = "")
if node.right != ".":
midorder(tree[node.right])
def postorder(node):
if node.left != ".":
postorder(tree[node.left])
if node.right != ".":
postorder(tree[node.right])
print(node.item, end = "")
class Node:
def __init__(self, item, left, right):
self.item = item
self.left = left
self.right = right
N = int(input())
tree = {}
for i in range(N):
temp = input().split()
tree[temp[0]] = Node(item = temp[0] , left = temp[1], right = temp[2])
preorder(tree['A'])
print()
midorder(tree['A'])
print()
postorder(tree['A'])