-
Notifications
You must be signed in to change notification settings - Fork 43
/
binary-tree-paths.py
343 lines (301 loc) · 9.61 KB
/
binary-tree-paths.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
"""
257. Binary Tree Paths
Easy
Given the root of a binary tree, return all root-to-leaf paths in any order.
A leaf is a node with no children.
Example 1:
Input: root = [1,2,3,null,5]
Output: ["1->2->5","1->3"]
Example 2:
Input: root = [1]
Output: ["1"]
Constraints:
The number of nodes in the tree is in the range [1, 100].
-100 <= Node.val <= 100
"""
# V0
# IDEA : BFS
class Solution(object):
def binaryTreePaths(self, root):
# edge case
if not root:
return
# bfs
res = []
cache = ""
q = [[root, cache]]
while q:
#for i in range(len(q)):
tmp_root, cache = q.pop(0)
# NOTE : this condition
if tmp_root and not tmp_root.left and not tmp_root.right:
cache += ("->" + str(tmp_root.val))
res.append(cache)
"""
NOTE !!! we append tmp_root.val to cache within left, right sub tree op
"""
if tmp_root.left:
q.append([tmp_root.left, cache + "->" + str(tmp_root.val)])
if tmp_root.right:
q.append([tmp_root.right, cache + "->" + str(tmp_root.val)])
#print ("res = " + str(res))
#return [x.strip("->") for x in res]
return ["->".join(x.split("->")[1:]) for x in res]
# V0
# IDEA : BFS
class Solution(object):
def binaryTreePaths(self, root):
# edge case
if not root:
return []
cache = ""
res = []
q = [[cache, root]]
while q:
#for i in range(len(q)):
cache, tmp = q.pop(0)
"""
NOTE this !!!
1) condition : if tmp and not tmp.left and not tmp.right
2) we need to append cache + str(tmp.val) to res
-> since we also need to collect "bottom" sbt-tree's val
"""
if tmp and not tmp.left and not tmp.right:
res.append(cache + str(tmp.val))
"""
NOTE !!! we append tmp_root.val to cache within left, right sub tree op
"""
if tmp.left:
q.append([ cache + "{}->".format(tmp.val), tmp.left])
if tmp.right:
q.append([ cache + "{}->".format(tmp.val), tmp.right])
#print ("res = " + str(res))
return res
# V0'
# IDEA : BFS
class Solution:
def binaryTreePaths(self, root):
res = []
### NOTE : we set q like this : [[root, cur]]
cur = ""
q = [[root, cur]]
while q:
for i in range(len(q)):
node, cur = q.pop(0)
### NOTE : if node exist, but no sub tree (i.e. not root.left and not root.right)
# -> append cur to result
if node:
if not node.left and not node.right:
res.append(cur + str(node.val))
### NOTE : we keep cur to left sub tree
if node.left:
q.append((node.left, cur + str(node.val) + '->'))
### NOTE : we keep cur to left sub tree
if node.right:
q.append((node.right, cur + str(node.val) + '->'))
return res
# V0'
# IDEA : DFS
class Solution:
def binaryTreePaths(self, root):
ans = []
def dfs(r, tmp):
if r.left:
dfs(r.left, tmp + [str(r.left.val)])
if r.right:
dfs(r.right, tmp + [str(r.right.val)])
if not r.left and not r.right:
ans.append('->'.join(tmp))
if not root:
return []
dfs(root, [str(root.val)])
return ans
# V1
# IDEA : DFS
# https://leetcode.com/problems/binary-tree-paths/discuss/68335/Python-DFS
class Solution:
def binaryTreePaths(self, root):
ans = []
def dfs(r, tmp):
if r.left:
dfs(r.left, tmp + [str(r.left.val)])
if r.right:
dfs(r.right, tmp + [str(r.right.val)])
if not r.left and not r.right:
ans.append('->'.join(tmp))
if not root:
return []
dfs(root, [str(root.val)])
return ans
# V1'
# IDEA : DFS
# https://leetcode.com/problems/binary-tree-paths/discuss/237550/Python-solution
class Solution:
def binaryTreePaths(self, root):
def dfs(root):
if not root:
return
if not root.left and not root.right:
tmp.append(str(root.val))
res.append("->".join(tmp))
tmp.pop()
return
tmp.append(str(root.val))
dfs(root.left)
dfs(root.right)
tmp.pop()
tmp = []
res = []
dfs(root)
return res
# V1''
# https://blog.csdn.net/coder_orz/article/details/51706119
# DEMO
# In [14]: x
# Out[14]: [1, 2, 3]
# In [15]: res = []
# In [16]: res.append('->'.join("123"))
# In [17]: res
# Out[17]: ['1->2->3']
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# @param {TreeNode} root
# @return {string[]}
def binaryTreePaths(self, root):
res, path_list = [], []
self.dfs(root, path_list, res)
return res
def dfs(self, root, path_list, res):
if not root:
return
path_list.append(str(root.val))
if not root.left and not root.right:
res.append('->'.join(path_list))
if root.left:
self.dfs(root.left, path_list, res)
if root.right:
self.dfs(root.right, path_list, res)
path_list.pop()
# V1'''
# https://blog.csdn.net/coder_orz/article/details/51706119
class Solution:
# @param {TreeNode} root
# @return {string[]}
def binaryTreePaths(self, root):
res = []
if not root:
return res
if not root.left and not root.right:
res.append(str(root.val))
return res
for path in self.binaryTreePaths(root.left):
res.append(str(root.val) + '->' + path)
for path in self.binaryTreePaths(root.right):
res.append(str(root.val) + '->' + path)
return res
# V1'''''
# https://blog.csdn.net/coder_orz/article/details/51706119
# IDEA : BFS (RECURSION)
class Solution:
# @param {TreeNode} root
# @return {string[]}
def binaryTreePaths(self, root):
res, stack = [], [(root, '')]
while stack:
node, curs = stack.pop()
if node:
if not node.left and not node.right:
res.append(curs + str(node.val))
stack.append((node.left, curs + str(node.val) + '->'))
stack.append((node.right, curs + str(node.val) + '->'))
return res
# V1'''''''
# https://blog.csdn.net/coder_orz/article/details/51706119
# IDEA : BFS
class Solution:
# @param {TreeNode} root
# @return {string[]}
def binaryTreePaths(self, root):
res, queue = [], [(root, '')]
while queue:
node, curs = queue.pop()
if node:
if not node.left and not node.right:
res.append(curs + str(node.val))
queue.insert(0, (node.left, curs + str(node.val) + '->'))
queue.insert(0, (node.right, curs + str(node.val) + '->'))
return res
# V1'''''''''
# https://www.jiuzhang.com/solution/binary-tree-paths/#tag-highlight-lang-python
class Solution:
"""
@param root: the root of the binary tree
@return: all root-to-leaf paths
"""
def binaryTreePaths(self, root):
if root is None:
return []
result = []
self.dfs(root, [str(root.val)], result)
return result
def dfs(self, node, path, result):
if node.left is None and node.right is None:
result.append('->'.join(path))
return
if node.left:
path.append(str(node.left.val))
self.dfs(node.left, path, result)
path.pop()
if node.right:
path.append(str(node.right.val))
self.dfs(node.right, path, result)
path.pop()
# V1''''''''
# https://www.jiuzhang.com/solution/binary-tree-paths/#tag-highlight-lang-python
class Solution:
"""
@param root: the root of the binary tree
@return: all root-to-leaf paths
"""
def binaryTreePaths(self, root):
if root is None:
return []
if root.left is None and root.right is None:
return [str(root.val)]
leftPaths = self.binaryTreePaths(root.left)
rightPaths = self.binaryTreePaths(root.right)
paths = []
for path in leftPaths + rightPaths:
paths.append(str(root.val) + '->' + path)
return paths
# V2
# Time: O(n * h)
# Space: O(h)
class Solution(object):
# @param {TreeNode} root
# @return {string[]}
def binaryTreePaths(self, root):
result, path = [], []
self.binaryTreePathsRecu(root, path, result)
return result
def binaryTreePathsRecu(self, node, path, result):
if node is None:
return
if node.left is node.right is None:
ans = ""
for n in path:
ans += str(n.val) + "->"
result.append(ans + str(node.val))
if node.left:
path.append(node)
self.binaryTreePathsRecu(node.left, path, result)
path.pop()
if node.right:
path.append(node)
self.binaryTreePathsRecu(node.right, path, result)
path.pop()