-
Notifications
You must be signed in to change notification settings - Fork 43
/
flatten-binary-tree-to-linked-list.py
148 lines (130 loc) · 4.06 KB
/
flatten-binary-tree-to-linked-list.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# V0
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/70241424
class Solution(object):
def flatten(self, root):
"""
:type root: TreeNode
:rtype: void Do not return anything, modify root in-place instead.
"""
res = []
self.preOrder(root, res)
for i in range(len(res) - 1):
res[i].left = None
res[i].right = res[i + 1]
def preOrder(self, root, res):
if not root: return
res.append(root)
self.preOrder(root.left, res)
self.preOrder(root.right, res)
# V1'
# http://bookshadow.com/weblog/2016/09/02/leetcode-flatten-binary-tree-to-linked-list/
class Solution(object):
def flatten(self, root):
"""
:type root: TreeNode
:rtype: void Do not return anything, modify root in-place instead.
"""
pointer = dummy = TreeNode(None)
stack = [root]
while stack:
top = stack.pop()
if not top: continue
stack.append(top.right)
stack.append(top.left)
pointer.right = top
pointer.left = None
pointer = top
# V1''
# http://bookshadow.com/weblog/2016/09/02/leetcode-flatten-binary-tree-to-linked-list/
class Solution(object):
def flatten(self, root):
"""
:type root: TreeNode
:rtype: void Do not return anything, modify root in-place instead.
"""
self.pointer = None
def traverse(root):
if not root: return
traverse(root.right)
traverse(root.left)
root.right = self.pointer
root.left = None
self.pointer = root
traverse(root)
# V1'''
# https://www.jiuzhang.com/solution/flatten-binary-tree-to-linked-list/#tag-highlight-lang-python
class Solution:
last_node = None
"""
@param root: a TreeNode, the root of the binary tree
@return: nothing
"""
def flatten(self, root):
if root is None:
return
if self.last_node is not None:
self.last_node.left = None
self.last_node.right = root
self.last_node = root
right = root.right
self.flatten(root.left)
self.flatten(right)
# V1''''
# https://www.jiuzhang.com/solution/flatten-binary-tree-to-linked-list/#tag-highlight-lang-python
class Solution:
"""
@param root: a TreeNode, the root of the binary tree
@return: nothing
"""
def flatten(self, root):
self.helper(root)
# restructure and return last node in preorder
def helper(self, root):
if root is None:
return None
left_last = self.helper(root.left)
right_last = self.helper(root.right)
# connect
if left_last is not None:
left_last.right = root.right
root.right = root.left
root.left = None
if right_last is not None:
return right_last
if left_last is not None:
return left_last
return root
# V2
# Time: O(n)
# Space: O(h), h is height of binary tree
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
# @param root, a tree node
# @return nothing, do it in place
def flatten(self, root):
self.flattenRecu(root, None)
def flattenRecu(self, root, list_head):
if root:
list_head = self.flattenRecu(root.right, list_head)
list_head = self.flattenRecu(root.left, list_head)
root.right = list_head
root.left = None
return root
else:
return list_head
class Solution2(object):
list_head = None
# @param root, a tree node
# @return nothing, do it in place
def flatten(self, root):
if root:
self.flatten(root.right)
self.flatten(root.left)
root.right = self.list_head
root.left = None
self.list_head = root