Skip to content

Commit fc4085c

Browse files
solves merge two binary trees
1 parent 2536151 commit fc4085c

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@
160160
| 599 | [Minimum Index Sum of 2 Lists](https://leetcode.com/problems/minimum-index-sum-of-two-lists) | [![Java](assets/java.png)](src/MinimumIndexSumOfTwoLists.java) [![Python](assets/python.png)](python/minimum_index_sum_of_two_lists.py) |
161161
| 604 | 🔒 [Design Compressed String Iterator](https://leetcode.com/problems/design-compressed-string-iterator) | |
162162
| 605 | [Can Place Flowers](https://leetcode.com/problems/can-place-flowers) | [![Java](assets/java.png)](src/CanPlaceFlowers.java) [![Python](assets/python.png)](python/can_place_flowers.py) |
163-
| 606 | [Construct String from Binary Tree](https://leetcode.com/problems/construct-string-from-binary-tree) | [![Java](assets/java.png)](src/ConstructStringFromBinaryTree.java) [![Python](assets/python.png)](python/construct_string_from_binary_tree.py)|
164-
| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees) | |
163+
| 606 | [Construct String from Binary Tree](https://leetcode.com/problems/construct-string-from-binary-tree) | [![Java](assets/java.png)](src/ConstructStringFromBinaryTree.java) [![Python](assets/python.png)](python/construct_string_from_binary_tree.py) |
164+
| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees) | [![Java](assets/java.png)](src/MergeTwoBinaryTrees.java) [![Python](assets/python.png)](python/merge_two_binary_trees.py) |
165165
| 624 | [Maximum Distance in Arrays](https://leetcode.com/problems/maximum-distance-in-arrays) | |
166166
| 628 | [Maximum Product of Three Numbers](https://leetcode.com/problems/maximum-product-of-three-numbers) | |
167167
| 633 | [Sum Square Numbers](https://leetcode.com/problems/sum-of-square-numbers) | |

python/merge_two_binary_trees.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
8+
9+
class Solution:
10+
def mergeTrees(self, root1: TreeNode, root2: TreeNode) -> TreeNode:
11+
if root1 is None: return root2
12+
if root2 is None: return root1
13+
root1.val += root2.val
14+
root1.left = self.mergeTrees(root1.left, root2.left)
15+
root1.right = self.mergeTrees(root1.right, root2.right)
16+
return root1

src/MergeTwoBinaryTrees.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class MergeTwoBinaryTrees {
2+
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
3+
if (root1 == null) return root2;
4+
if (root2 == null) return root1;
5+
root1.val += root2.val;
6+
root1.left = mergeTrees(root1.left, root2.left);
7+
root1.right = mergeTrees(root1.right, root2.right);
8+
return root1;
9+
}
10+
11+
private void mergeTrees(TreeNode result, TreeNode root1, TreeNode root2) {
12+
if (root1 == null && root2 == null) return;
13+
if (root1 == null) result.val = root2.val;
14+
else if (root2 == null) result.val = root1.val;
15+
else result.val = root1.val + root2.val;
16+
if ((root1 != null && root1.left != null) || (root2 != null && root2.left != null)) {
17+
result.left = new TreeNode();
18+
mergeTrees(result.left, root1 == null ? null : root1.left, root2 == null ? null : root2.left);
19+
}
20+
if ((root1 != null && root1.right != null) || (root2 != null && root2.right != null)) {
21+
result.right = new TreeNode();
22+
mergeTrees(result.right, root1 == null ? null : root1.right, root2 == null ? null : root2.right);
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)