Skip to content

Commit 7c7b988

Browse files
committed
Merge Two Binary Trees
1 parent d51048e commit 7c7b988

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Salesforce/Problem#422.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
Write a program to merge two binary trees. Each node in the new tree should hold a
3+
value equal to the sum of the values of the corresponding nodes of the input trees.
4+
If only one input tree has a node in a given position, the corresponding node in
5+
the new tree should match that input node.
6+
"""
7+
def mergeTrees(t1: TreeNode, t2: TreeNode) -> TreeNode:
8+
if not t1 or not t2:
9+
return t1 or t2
10+
t1.val += t2.val
11+
t1.left = mergeTrees(t1.left, t2.left)
12+
t1.right = mergeTrees(t1.right, t2.right)
13+
return t1

0 commit comments

Comments
 (0)