File tree 1 file changed +25
-0
lines changed
1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change
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.
You can’t perform that action at this time.
0 commit comments