Skip to content

Commit 723cec8

Browse files
authored
Create Merge Two Binary Trees.java
1 parent 0e93e0e commit 723cec8

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

easy/Merge Two Binary Trees.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//617. Merge Two Binary Trees
2+
class Solution {
3+
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
4+
if (root1 == null && root2 == null)
5+
return null;
6+
final int val = (root1 == null ? 0 : root1.val) + (root2 == null ? 0 : root2.val);
7+
TreeNode root = new TreeNode(val);
8+
root.left = mergeTrees(root1 == null ? null : root1.left, root2 == null ? null : root2.left);
9+
root.right = mergeTrees(root1 == null ? null : root1.right, root2 == null ? null : root2.right);
10+
return root;
11+
}
12+
}

0 commit comments

Comments
 (0)