-
Notifications
You must be signed in to change notification settings - Fork 43
/
merge-two-binary-trees.py
521 lines (463 loc) · 14.8 KB
/
merge-two-binary-trees.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
"""
617. Merge Two Binary Trees
Easy
You are given two binary trees root1 and root2.
Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of the new tree.
Return the merged tree.
Note: The merging process must start from the root nodes of both trees.
Example 1:
Input: root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7]
Output: [3,4,5,5,4,null,7]
Example 2:
Input: root1 = [1], root2 = [1,2]
Output: [2,2]
Constraints:
The number of nodes in both trees is in the range [0, 2000].
-104 <= Node.val <= 104
"""
# V0
# IDEA : recursive
class Solution(object):
def mergeTrees(self, root1, root2):
def help(root1, root2):
if not root1 and not root2:
return
if root1 and root2:
newT = TreeNode(root1.val + root2.val)
newT.left = help(root1.left, root2.left)
newT.right = help(root1.right, root2.right)
return newT
if not root1 or not root2:
return root1 if root1 else root2
res = help(root1, root2)
return res
# V0'
# IDEA : ITERATION, BFS
class Solution(object):
def mergeTrees(self, t1, t2):
dummy = TreeNode(0)
stack = [(t1, t2, dummy, 'l')]
while stack:
n1, n2, parent, lr = stack.pop()
n = TreeNode((n1.val if n1 else 0 ) + (n2.val if n2 else 0 )) if n1 or n2 else None
if lr == 'l':
parent.left = n
else:
parent.right = n
if n1 or n2:
stack.append((n1 and n1.left, n2 and n2.left, n, 'l'))
stack.append((n1 and n1.right, n2 and n2.right, n, 'r'))
return dummy.left
# V0
# IDEA : recursive
class Solution:
def mergeTrees(self, t1, t2):
return self.dfs(t1,t2)
def dfs(self, t1, t2):
if not t1 and not t2:
return
if t1 and t2:
### NOTE here
newT = TreeNode(t1.val + t2.val)
newT.right = self.mergeTrees(t1.right, t2.right)
newT.left = self.mergeTrees(t1.left, t2.left)
return newT
### NOTE here
else:
return t1 or t2
# V0'
# IDEA : DFS + BACKTRACK
class Solution:
def mergeTrees(self, t1, t2):
return self.dfs(t1,t2)
def dfs(self, t1, t2):
if t1 and t2:
newT = TreeNode(t1.val + t2.val)
newT.left = self.mergeTrees(t1.left, t2.left)
newT.right = self.mergeTrees(t1.right, t2.right)
return newT
else:
return t1 or t2
# V0'
class Solution:
def mergeTrees(self, t1, t2):
if t1 and t2:
newT = TreeNode(t1.val + t2.val)
newT.left = self.mergeTrees(t1.left, t2.left)
newT.right = self.mergeTrees(t1.right, t2.right)
return newT
else:
return t1 or t2
# V1
# IDEA : DFS
# https://blog.csdn.net/fuxuemingzhu/article/details/79052953
class Solution:
def mergeTrees(self, t1, t2):
"""
:type t1: TreeNode
:type t2: TreeNode
:rtype: TreeNode
"""
if t1 and t2:
newT = TreeNode(t1.val + t2.val)
newT.left = self.mergeTrees(t1.left, t2.left)
newT.right = self.mergeTrees(t1.right, t2.right)
return newT
else:
return t1 or t2
# V1
# https://leetcode.com/problems/merge-two-binary-trees/discuss/159138/Python-solution
# IDEA : resursive
class Solution(object):
def mergeTrees(self, t1, t2):
if t1 == None and t2 == None:
return None
elif t1 == None:
return t2
elif t2 == None:
return t1
new_root = TreeNode(t1.val + t2.val)
left = self.mergeTrees(t1.left, t2.left)
right = self.mergeTrees(t1.right, t2.right)
new_root.left = left
new_root.right = right
return new_root
# V1
# IDEA : iterative
# https://leetcode.com/problems/merge-two-binary-trees/discuss/121170/Python-iterative
class Solution(object):
def mergeTrees(self, t1, t2):
dummy = TreeNode(0)
stack = [(t1, t2, dummy, 'l')]
while stack:
n1, n2, parent, lr = stack.pop()
n = TreeNode((n1.val if n1 else 0 ) + (n2.val if n2 else 0 )) if n1 or n2 else None
if lr == 'l':
parent.left = n
else:
parent.right = n
if n1 or n2:
stack.append((n1 and n1.left, n2 and n2.left, n, 'l'))
stack.append((n1 and n1.right, n2 and n2.right, n, 'r'))
return dummy.left
### Test case : dev
# V1
# https://leetcode.com/problems/merge-two-binary-trees/solution/
# IDEA : resursive
# java
# /**
# * Definition for a binary tree node.
# * public class TreeNode {
# * int val;
# * TreeNode left;
# * TreeNode right;
# * TreeNode(int x) { val = x; }
# * }
# */
# public class Solution {
# public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
# if (t1 == null)
# return t2;
# if (t2 == null)
# return t1;
# t1.val += t2.val;
# t1.left = mergeTrees(t1.left, t2.left);
# t1.right = mergeTrees(t1.right, t2.right);
# return t1;
# }
# }
# V1
# https://leetcode.com/problems/merge-two-binary-trees/solution/
# IDEA : Iterative
# java
# /**
# * Definition for a binary tree node.
# * public class TreeNode {
# * int val;
# * TreeNode left;
# * TreeNode right;
# * TreeNode(int x) { val = x; }
# * }
# */
# public class Solution {
# public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
# if (t1 == null)
# return t2;
# Stack < TreeNode[] > stack = new Stack < > ();
# stack.push(new TreeNode[] {t1, t2});
# while (!stack.isEmpty()) {
# TreeNode[] t = stack.pop();
# if (t[0] == null || t[1] == null) {
# continue;
# }
# t[0].val += t[1].val;
# if (t[0].left == null) {
# t[0].left = t[1].left;
# } else {
# stack.push(new TreeNode[] {t[0].left, t[1].left});
# }
# if (t[0].right == null) {
# t[0].right = t[1].right;
# } else {
# stack.push(new TreeNode[] {t[0].right, t[1].right});
# }
# }
# return t1;
# }
# }
# V1'
# https://www.polarxiong.com/archives/LeetCode-617-merge-two-binary-trees.html
class Solution:
def mergeTrees(self, t1, t2):
"""
:type t1: TreeNode
:type t2: TreeNode
:rtype: TreeNode
"""
if t1 is not None and t2 is not None:
t1.left = self.mergeTrees(t1.left, t2.left)
t1.right = self.mergeTrees(t1.right, t2.right)
t1.val += t2.val
return t1
return t1 if t2 is None else t2
# V1''
# https://www.jiuzhang.com/solution/merge-two-binary-trees/#tag-highlight-lang-python
class Solution:
"""
@param t1: the root of the first tree
@param t2: the root of the second tree
@return: the new binary tree after merge
"""
def mergeTrees(self, t1, t2):
# Write your code here
if t1 is None:
return t2
if t2 is None:
return t1
t3 = TreeNode(t1.val + t2.val)
t3.left = self.mergeTrees(t1.left, t2.left)
t3.right = self.mergeTrees(t1.right, t2.right)
return t3
# V1'''
# https://leetcode.com/problems/merge-two-binary-trees/discuss/124537/Python-recursive-iterative-DFS-BFS-solutions
# IDEA : BFS
from collections import deque
class Solution:
def mergeTrees(self, t1, t2):
"""
:type t1: TreeNode
:type t2: TreeNode
:rtype: TreeNode
"""
if t1 is None and t2 is None:
return
if t1 is None:
return t2
if t2 is None:
return t1
q1, q2, q = deque(), deque(), deque()
tRoot = TreeNode(t1.val + t2.val)
t = tRoot
q1.append(t1)
q2.append(t2)
q.append(t)
while len(q1) > 0:
t1, t2, t = q1.popleft(), q2.popleft(), q.popleft()
if t1.left is None and t2.left is None:
pass
elif t1.left is None:
t.left = t2.left
elif t2.left is None:
t.left = t1.left
else:
t.left = TreeNode(t1.left.val + t2.left.val)
q1.append(t1.left)
q2.append(t2.left)
q.append(t.left)
if t1.right is None and t2.right is None:
pass
elif t1.right is None:
t.right = t2.right
elif t2.right is None:
t.right = t1.right
else:
t.right = TreeNode(t1.right.val + t2.right.val)
q1.append(t1.right)
q2.append(t2.right)
q.append(t.right)
return tRoot
# V1'''''''
# https://leetcode.com/problems/merge-two-binary-trees/discuss/124537/Python-recursive-iterative-DFS-BFS-solutions
# IDEA : RECURSION PRE-ORDER
class Solution:
def mergeTrees(self, t1, t2):
"""
:type t1: TreeNode
:type t2: TreeNode
:rtype: TreeNode
"""
if t1 is None and t2 is None:
return
if t1 is None:
return t2
if t2 is None:
return t1
t = TreeNode(t1.val + t2.val)
t.left = self.mergeTrees(t1.left, t2.left)
t.right = self.mergeTrees(t1.right, t2.right)
return t
# V1''''''''
# https://leetcode.com/problems/merge-two-binary-trees/discuss/124537/Python-recursive-iterative-DFS-BFS-solutions
# IDEA : Recursion in-order
class Solution:
def mergeTrees(self, t1, t2):
"""
:type t1: TreeNode
:type t2: TreeNode
:rtype: TreeNode
"""
if t1 is None and t2 is None:
return
if t1 is None:
return t2
if t2 is None:
return t1
tLeft = self.mergeTrees(t1.left, t2.left)
t = TreeNode(t1.val + t2.val)
t.left = tLeft
t.right = self.mergeTrees(t1.right, t2.right)
return t
# V1''''''''
# https://leetcode.com/problems/merge-two-binary-trees/discuss/124537/Python-recursive-iterative-DFS-BFS-solutions
# IDEA : Recursion post-order
class Solution:
def mergeTrees(self, t1, t2):
"""
:type t1: TreeNode
:type t2: TreeNode
:rtype: TreeNode
"""
if t1 is None and t2 is None:
return
if t1 is None:
return t2
if t2 is None:
return t1
tLeft = self.mergeTrees(t1.left, t2.left)
tRight = self.mergeTrees(t1.right, t2.right)
t = TreeNode(t1.val + t2.val)
t.left, t.right = tLeft, tRight
return t
# V1''''''''''
# https://leetcode.com/problems/merge-two-binary-trees/discuss/124537/Python-recursive-iterative-DFS-BFS-solutions
# Iterative in-order
class Solution:
def mergeTrees(self, t1, t2):
"""
:type t1: TreeNode
:type t2: TreeNode
:rtype: TreeNode
"""
if t1 is None and t2 is None:
return
if t1 is None:
return t2
if t2 is None:
return t1
stack1 = []
stack2 = []
stack = []
tRoot = TreeNode(t1.val + t2.val)
t = tRoot
while len(stack1) > 0 or t1 is not None:
while t1 is not None and t2 is not None:
stack1.append(t1)
stack2.append(t2)
stack.append(t)
# Note: need to delay going to left if one tree is null
if t1.left is not None and t2.left is not None:
t.left = TreeNode(t1.left.val + t2.left.val)
t = t.left
t1, t2 = t1.left, t2.left
if t1 is not None:
t.left = t1
if t2 is not None:
t.left = t2
t1, t2, t = stack1.pop(), stack2.pop(), stack.pop()
if t1.right is None and t2.right is None:
t1 = t2 = None
elif t1.right is None:
t.right = t2.right
t1 = t2 = None
elif t2.right is None:
t.right = t1.right
t1 = t2 = None
else:
t.right = TreeNode(t1.right.val + t2.right.val)
t1, t2, t = t1.right, t2.right, t.right
return tRoot
# V1'''''''''''''''
# https://leetcode.com/problems/merge-two-binary-trees/discuss/124537/Python-recursive-iterative-DFS-BFS-solutions
# IDEA : Iterative pre-order
class Solution:
def mergeTrees(self, t1, t2):
"""
:type t1: TreeNode
:type t2: TreeNode
:rtype: TreeNode
"""
if t1 is None and t2 is None:
return
if t1 is None:
return t2
if t2 is None:
return t1
stack1 = [t1]
stack2 = [t2]
tRoot = TreeNode(t1.val + t2.val)
stack = [tRoot]
while len(stack) > 0:
t1 = stack1.pop()
t2 = stack2.pop()
t = stack.pop()
if t1.right is None and t2.right is None:
pass
elif t1.right is None:
t.right = t2.right
elif t2.right is None:
t.right = t1.right
else:
t.right = TreeNode(t1.right.val + t2.right.val)
stack1.append(t1.right)
stack2.append(t2.right)
stack.append(t.right)
if t1.left is None and t2.left is None:
pass
elif t1.left is None:
t.left = t2.left
elif t2.left is None:
t.left = t1.left
else:
t.left = TreeNode(t1.left.val + t2.left.val)
stack1.append(t1.left)
stack2.append(t2.left)
stack.append(t.left)
return tRoot
# V2
# Time: O(n)
# Space: O(h)
class Solution(object):
def mergeTrees(self, t1, t2):
"""
:type t1: TreeNode
:type t2: TreeNode
:rtype: TreeNode
"""
if t1 is None:
return t2
if t2 is None:
return t1
t1.val += t2.val
t1.left = self.mergeTrees(t1.left, t2.left)
t1.right = self.mergeTrees(t1.right, t2.right)
return t1