forked from maze-runnar/interview-preparation-kit
-
Notifications
You must be signed in to change notification settings - Fork 4
/
SwapNodes.py
72 lines (56 loc) · 1.77 KB
/
SwapNodes.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
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
#!/bin/python3
import os
import sys
import collections
# Complete the swapNodes function below.
class Node:
def __init__(self, d):
self.data = d
def build_tree(indexes):
f = lambda x: None if x == -1 else Node(x)
children = [list(map(f,x)) for x in indexes]
nodes = {n.data: n for n in filter(None, sum(children, []))}
nodes[1] = Node(1)
for idx, child_pair in enumerate(children):
nodes[idx+1].left = child_pair[0]
nodes[idx+1].right = child_pair[1]
return nodes[1]
def inorder(root):
stack = []
curr = root
while stack or curr:
if curr:
stack.append(curr)
curr = curr.left
elif stack:
curr = stack.pop()
yield curr.data
curr = curr.right
def swapNodes(indexes, queries):
root = build_tree(indexes)
for k in queries:
h = 1
q = collections.deque([root])
while q:
for _ in range(len(q)):
node = q.popleft()
if h % k == 0:
node.left, node.right = node.right, node.left
q += filter(None, (node.left, node.right))
h += 1
yield inorder(root)
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input())
indexes = []
for _ in range(n):
indexes.append(list(map(int, input().rstrip().split())))
queries_count = int(input())
queries = []
for _ in range(queries_count):
queries_item = int(input())
queries.append(queries_item)
result = swapNodes(indexes, queries)
fptr.write('\n'.join([' '.join(map(str, x)) for x in result]))
fptr.write('\n')
fptr.close()