Skip to content

Commit 7810fc8

Browse files
committed
617. Merge Two Binary Trees
1 parent 6681bf2 commit 7810fc8

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ The project is divided into two parts: `structure` and `solution`.
8383
| 515 | [Find Largest Value in Each Tree Row](https://leetcode.com/problems/find-largest-value-in-each-tree-row/) | Medium | [FindLargestValueInEachTreeRow.java](src/leetcode/solution/tree/FindLargestValueInEachTreeRow.java) | BFS \|DFS |
8484
| 404 | [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | Easy | [SumOfLeftLeaves.java](src/leetcode/solution/tree/SumOfLeftLeaves.java) | DFS |
8585
| 513 | [Find Bottom Left Tree Value](https://leetcode.com/problems/find-bottom-left-tree-value/) | Medium | [FindBottomLeftTreeValue.java](src/leetcode/solution/tree/FindBottomLeftTreeValue.java) | DFS |
86+
| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/) | Easy | [MergeTwoBinaryTrees.java](src/leetcode/solution/tree/MergeTwoBinaryTrees.java) | DFS |
8687
| 662 | [Maximum Width of Binary Tree](https://leetcode.com/problems/maximum-width-of-binary-tree/) | Medium | [MaximumWidthOfBinaryTree.java](src/leetcode/solution/tree/MaximumWidthOfBinaryTree.java) | BFS \|DFS + The relationship of the index between the father node and the child node. |
8788
| 979 | [Distribute Coins in Binary Tree](https://leetcode.com/problems/distribute-coins-in-binary-tree/) | Medium | [DistributeCoinsInBinaryTree.java](src/leetcode/solution/tree/DistributeCoinsInBinaryTree.java) | DFS |
8889
| 1302 | [Deepest Leaves Sum](https://leetcode.com/problems/deepest-leaves-sum/) | Medium | [DeepestLeavesSum.java](src/leetcode/solution/tree/DeepestLeavesSum.java) | BFS \| DFS |
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package leetcode.solution.tree;
2+
3+
import leetcode.structure.TreeNode;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
/**
9+
* 617. Merge Two Binary Trees
10+
*/
11+
public class MergeTwoBinaryTrees {
12+
13+
public static void main(String[] args) {
14+
Integer[] array1 = {1, 2, 3, null, 5, null, 4};
15+
TreeNode r1 = TreeNode.constructTree(array1);
16+
Integer[] array2 = {1, 2, 3, null, 5, null, 4};
17+
TreeNode r2 = TreeNode.constructTree(array2);
18+
19+
MergeTwoBinaryTrees view = new MergeTwoBinaryTrees();
20+
TreeNode ans = view.mergeTrees(r1, r2);
21+
System.out.println(TreeNode.print(ans));
22+
}
23+
24+
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
25+
if (root1 == null) {
26+
return root2;
27+
}
28+
29+
if (root2 == null) {
30+
return root1;
31+
}
32+
33+
root1.val += root2.val;
34+
35+
root1.left = mergeTrees(root1.left, root2.left);
36+
root1.right = mergeTrees(root1.right, root2.right);
37+
38+
39+
return root1;
40+
}
41+
42+
}

0 commit comments

Comments
 (0)