-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvert_binary_tree.py
More file actions
70 lines (60 loc) · 2.14 KB
/
invert_binary_tree.py
File metadata and controls
70 lines (60 loc) · 2.14 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
68
69
70
class Node():
def __init__(self, value): # O(1)
self.value = value # O(1)
self.left = None # O(1)
self.right = None # O(1)
class Tree():
def __init__(self, value=None): # O(1)
if value is not None: # O(1)
self.root = Node(value) # O(1)
else: # O(1)
self.root = None # O(1)
def convert_array_to_nodes(array, index, tree=None, position=None):
"""
Convert a binary tree in array form to one in node form
"""
if index < len(array):
node = Node(array[index])
if position:
setattr(tree, position, node)
else:
tree = node
convert_array_to_nodes(array, 2 * index + 1, tree, 'left')
convert_array_to_nodes(array, 2 * index + 2, tree, 'right')
if index:
return None
return tree
def convert_nodes_to_array(tree, array=None, position=0):
"""
Convert a binary tree in node form to array
"""
if tree:
if array:
array.append(tree.value)
else:
array = [tree.value]
convert_nodes_to_array(tree.left, array, 2 * position + 1)
convert_nodes_to_array(tree.right, array, 2 * position + 2)
if position:
return None
return array
def invert_binary_tree(node):
if node:
invert_binary_tree(node.left)
invert_binary_tree(node.right)
node.left, node.right = node.right, node.left
def solution(array):
"""
Given a binary tree with nodes, invert it
# >>> solution([3, 2, 1])
# [3, 1, 2]
# >>> solution([2, 4, 5, 6, 3, 1])
# [2, 5, 4, 6, 1, 3]
"""
tree = Tree()
tree = convert_array_to_nodes(array, 0)
invert_binary_tree(tree)
return convert_nodes_to_array(tree)
if __name__ == "__main__":
import doctest
doctest.testmod()