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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
#!/usr/bin/env python
# Challenge 094
#
# TASK #2 > Binary Tree to Linked List
# Submitted by: Mohammad S Anwar
# You are given a binary tree.
#
# Write a script to represent the given binary tree as an object and flatten
# it to a linked list object. Finally print the linked list object.
#
# Example:
# Input:
#
# 1
# / \
# 2 3
# / \
# 4 5
# / \
# 6 7
#
# Output:
#
# 1 -> 2 -> 4 -> 5 -> 6 -> 7 -> 3
#
import fileinput
import re
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def __repr__(self):
return "Node(value: {}, left: {}, right: {})" \
.format(self.value, self.left, self.right)
def read_input():
lines = []
for line in fileinput.input():
lines.append(line)
return lines
def parse_subtree(lines, row, col):
def ch(row, col):
if row < 0 or row >= len(lines) or col < 0 or col >= len(lines[row]):
return ' '
else:
return lines[row][col]
tree = Node(int(lines[row][col]))
if ch(row + 1, col - 1) == '/':
tree.left = parse_subtree(lines, row + 2, col - 2)
if ch(row + 1, col + 1) == '\\':
tree.right = parse_subtree(lines, row + 2, col + 2)
return tree
def parse(lines):
found = re.search("^[ ]+\d", lines[0])
col = found.span()[1] - 1
return parse_subtree(lines, 0, col)
def flatten(tree):
if tree:
left = tree.left
right = tree.right
flatten(left)
flatten(right)
tree.left = None
tree.right = left
node = tree
while node.right:
node = node.right
node.right = right
def print_list(node):
output = ""
while node:
output += str(node.value) + " -> "
node = node.right
output = output[:-4]
print(output)
def solve():
lines = read_input()
tree = parse(lines)
flatten(tree)
print_list(tree)
solve()
|