-
Notifications
You must be signed in to change notification settings - Fork 0
/
swap-nodes-algo-hardish.py
68 lines (49 loc) · 1.49 KB
/
swap-nodes-algo-hardish.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
from copy import deepcopy
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def set_left(self, left):
self.left = left
def set_right(self, right):
self.right = right
def get_transverse(head, result):
if head == None:
return
get_transverse(head.left, result)
result.append(head.val)
get_transverse(head.right, result)
def build_tree(tree_indexes):
if len(tree_indexes) == 0:
return None
mini = min(tree_indexes)
minindex = tree_indexes.index(mini)
head = Node(mini)
head.set_left(build_tree(tree_indexes[:minindex]))
head.set_right(build_tree(tree_indexes[minindex+1:]))
return head
def get_next(tree_heads):
new_heads = []
for head in tree_heads:
if head.left != None:
new_heads.append(head.left)
if head.right != None:
new_heads.append(head.right)
return new_heads
def swap_tree_at_layer(tree, layer):
heads_at_layer = [tree]
for _ in range(layer-1):
heads_at_layer = get_next(heads_at_layer)
for head in heads_at_layer:
head.left, head.right = head.right, head.left
def swapNodes(indexes, queries):
t = build_tree(indexes)
final = []
for query in queries:
swap_tree_at_layer(t, query)
result = []
get_transverse(t, result)
final.append(deepcopy(result))
return final
print(swapNodes([6, 9, 4, 2, 1, 7, 5, 10, 8, 11, 3],[2,4]))