-
Notifications
You must be signed in to change notification settings - Fork 16
/
MergeTwoBinaryTrees.java
25 lines (24 loc) · 1.14 KB
/
MergeTwoBinaryTrees.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class MergeTwoBinaryTrees {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if (root1 == null) return root2;
if (root2 == null) return root1;
root1.val += root2.val;
root1.left = mergeTrees(root1.left, root2.left);
root1.right = mergeTrees(root1.right, root2.right);
return root1;
}
private void mergeTrees(TreeNode result, TreeNode root1, TreeNode root2) {
if (root1 == null && root2 == null) return;
if (root1 == null) result.val = root2.val;
else if (root2 == null) result.val = root1.val;
else result.val = root1.val + root2.val;
if ((root1 != null && root1.left != null) || (root2 != null && root2.left != null)) {
result.left = new TreeNode();
mergeTrees(result.left, root1 == null ? null : root1.left, root2 == null ? null : root2.left);
}
if ((root1 != null && root1.right != null) || (root2 != null && root2.right != null)) {
result.right = new TreeNode();
mergeTrees(result.right, root1 == null ? null : root1.right, root2 == null ? null : root2.right);
}
}
}