-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMerge-Two-Binary-Trees.py
More file actions
32 lines (29 loc) · 1.05 KB
/
Merge-Two-Binary-Trees.py
File metadata and controls
32 lines (29 loc) · 1.05 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
class Solution(object):
def mergeTrees(self, t1, t2):
a = {}
def check(root, stringVal='', second=True):
if (root == None):
return
if (stringVal in a):
a[stringVal]['v'] += root.val
else:
a[stringVal] = {}
a[stringVal]['v'] = root.val
a[stringVal]['l'] = root
root.left = check(root.left, (str((stringVal + 'L')) + '2'))
root.right = check(root.right, (str((stringVal + 'R')) + '2'))
return root
def fill(root, stringVal='', second=True):
r = a.get(stringVal, {'l': None})
if (r['l'] == None):
return
root = r['l']
root.val = r['v']
root.left = fill(root.left, (str((stringVal + 'L')) + '2'))
root.right = fill(root.right, (str((stringVal + 'R')) + '2'))
return root
b = check(t1, 'start')
b = check(t2, 'start')
a = fill(t2, 'start')
print a
return a