-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path617.py
39 lines (33 loc) · 929 Bytes
/
617.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
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
"""
新建节点来建立二叉树
"""
def newMerge(root1, root2):
if root1 is None and root2 is None:
return
if root1 is None:
return TreeNode(root2.val)
elif root2 is None:
return root1
else:
return TreeNode(root1.val + root2.val,root1.left,root1.right)
class Solution(object):
def mergeTrees(self, t1, t2):
"""
:type root1: TreeNode
:type root2: TreeNode
:rtype: TreeNode
"""
if not t1:
return t2
if not t2:
return t1
merged = TreeNode(t1.val + t2.val)
merged.left = self.mergeTrees(t1.left, t2.left)
merged.right = self.mergeTrees(t1.right, t2.right)
return merged