-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5639.py
More file actions
67 lines (46 loc) · 1.32 KB
/
5639.py
File metadata and controls
67 lines (46 loc) · 1.32 KB
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
61
62
63
64
65
66
67
import sys
sys.setrecursionlimit(10**9)
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def set_left(self, value):
self.left = Node(value)
def set_right(self, value):
self.right = Node(value)
class BinaryTree:
def __init__(self, root):
self.root = Node(root)
def insert(self, value):
temp = self.root
while True:
if value < temp.value:
if temp.left is not None:
temp = temp.left
else:
temp.set_left(value)
break
elif value > temp.value:
if temp.right is not None:
temp = temp.right
else:
temp.set_right(value)
break
result = []
def postorder(temp_node):
if temp_node.left is not None:
postorder(temp_node.left)
if temp_node.right is not None:
postorder(temp_node.right)
return result.append(temp_node.value)
binary_tree = BinaryTree(int(sys.stdin.readline().split()[0]))
for i in range(9999):
try:
n = int(sys.stdin.readline().split()[0])
binary_tree.insert(n)
except:
break
postorder(binary_tree.root)
for num in result:
print(num)