Skip to content

Commit 647c2d8

Browse files
committed
day 8
1 parent 449cfe7 commit 647c2d8

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

617. Merge Two Binary Trees.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
9+
10+
def dfsPreorder(root1,root2):
11+
if root1 == None and root2 == None:
12+
return
13+
elif root1 == None:
14+
return root2
15+
elif root2 == None:
16+
return root1
17+
else:
18+
root = TreeNode(root1.val + root2.val)
19+
root.left = dfsPreorder(root1.left, root2.left)
20+
root.right = dfsPreorder(root1.right, root2.right)
21+
return root
22+
23+
return dfsPreorder(root1, root2)

0 commit comments

Comments
 (0)