-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path617.java
More file actions
23 lines (23 loc) · 787 Bytes
/
617.java
File metadata and controls
23 lines (23 loc) · 787 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
if (t1 == null && t2 == null) return null;
if (t1 == null){
TreeNode root = new TreeNode(t2.val);
root.left = mergeTrees(null, t2.left);
root.right = mergeTrees(null, t2.right);
return root;
}
else if (t2 == null){
TreeNode root = new TreeNode(t1.val);
root.left = mergeTrees(t1.left, null);
root.right = mergeTrees(t1.right, null);
return root;
}
else {
TreeNode root = new TreeNode(t1.val + t2.val);
root.left = mergeTrees(t1.left, t2.left);
root.right = mergeTrees(t1.right, t2.right);
return root;
}
}
}