-
Notifications
You must be signed in to change notification settings - Fork 9
/
flatten.py
87 lines (70 loc) · 1.88 KB
/
flatten.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
## My Solution ##
class Solution(object):
def flatten(self, root):
"""
:type root: TreeNode
:rtype: void Do not return anything, modify root in-place instead.
"""
if not root:
return
# stack for storing the children we're overwriting
store = []
while root:
if root.right:
store.append(root.right)
if root.left:
root.right = root.left
root.left = None
elif store:
root.right = store.pop()
root = root.right
## Helpers ##
def preorder(node, ret):
if node:
ret.append(node.val)
preorder(node.left, ret)
preorder(node.right, ret)
# Create a BT like this:
# 1
# / \
# 2 5
# / \ / \
# 3 4 6 7
#
def create_preorder_binary_tree():
tree_nodes = [TreeNode(i) for i in xrange(1, 8)]
tree_nodes[0].left = tree_nodes[1]
tree_nodes[1].left = tree_nodes[2]
tree_nodes[1].right = tree_nodes[3]
tree_nodes[0].right = tree_nodes[4]
tree_nodes[4].left = tree_nodes[5]
tree_nodes[4].right = tree_nodes[6]
return tree_nodes[0]
## Tests ##
def test_create_preorder_binary_tree():
t = create_preorder_binary_tree()
ret = []
preorder(t, ret)
return ret == [1, 2, 3, 4, 5, 6, 7]
def test_regular_solution():
tree = create_preorder_binary_tree()
solution = Solution()
solution.flatten(tree)
checker = 1
while tree:
if tree.val != checker:
return False
tree = tree.right
checker += 1
return True
# TODO(albert): test more edge cases
def tests():
print(test_create_preorder_binary_tree())
print(test_regular_solution())
tests()