Skip to content

Commit 54da4e0

Browse files
committed
Adding Simple Solution for Problem - 617 - Merge Binary Trees
1 parent 45064f9 commit 54da4e0

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Merge_Binary_Trees/SimpleSolution.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
##==================================
2+
## Leetcode
3+
## Student: Vandit Jyotindra Gajjar
4+
## Year: 2020
5+
## Problem: 617
6+
## Problem Name: Merge Two Binary Tress
7+
##===================================
8+
#
9+
#Given two binary trees and imagine that when you put one of them to cover the other,
10+
#some nodes of the two trees are overlapped while the others are not.
11+
12+
#You need to merge them into a new binary tree. The merge rule is that if two nodes overlap,
13+
#then sum node values up as the new value of the merged node.
14+
#Otherwise, the NOT null node will be used as the node of new tree.
15+
16+
class Solution:
17+
def mergeTrees(self, t1, t2):
18+
if t1 is None: #Condition-check: If tree 1 is null, we simply merge/add tree 2.
19+
return t2
20+
if t2 is None: #Condition-check: If tree 2 is null, we simply merge/add tree 1.
21+
return t1
22+
t1.val = t1.val + t2.val #Now, we add root values and stores in tree 1's root. 'We'll return tree 1 at the end.'
23+
t1.left = self.mergeTrees(t1.left, t2.left) #Recursive call for left child
24+
t1.right = self.mergeTrees(t1.right, t2.right) #Recursive call for right child
25+
return t1 #Finally, we'll return t1.

0 commit comments

Comments
 (0)