-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path304.py
More file actions
23 lines (20 loc) · 708 Bytes
/
304.py
File metadata and controls
23 lines (20 loc) · 708 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if root1 is None:
return root2
if root2 is None:
return root1
new_node = None
if root1 != None and root2 != None:
new_node = TreeNode(
root1.val + root2.val,
self.mergeTrees(root1.left, root2.left),
self.mergeTrees(root1.right, root2.right)
)
return new_node