-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsolution.py
More file actions
38 lines (33 loc) · 949 Bytes
/
solution.py
File metadata and controls
38 lines (33 loc) · 949 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
36
37
38
# Solution for https://coderun.yandex.ru/problem/leaf-conclusion
# Other solutions: https://github.com/Melodiz/CodeRun
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def insert(root, value):
if root is None:
return Node(value)
if value < root.value:
root.left = insert(root.left, value)
elif value > root.value:
root.right = insert(root.right, value)
return root
def find_leaves(root, leaves):
if root is None:
return
if root.left and root.right:
leaves.append(root.value)
find_leaves(root.left, leaves)
find_leaves(root.right, leaves)
def main():
arr = list(map(int, input().split()))[:-1]
root = None
for num in arr:
root = insert(root, num)
leaves = []
find_leaves(root, leaves)
for val in sorted(leaves):
print(val)
if __name__ == "__main__":
main()