-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution_617.java
More file actions
45 lines (40 loc) · 1.2 KB
/
Solution_617.java
File metadata and controls
45 lines (40 loc) · 1.2 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.leetcode.algorithm.binarytree;
import com.leetcode.algorithm.TreeNode;
/**
* 合并两个二叉树
*/
public class Solution_617 {
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
if (t1 == null && t2 == null) {
return null;
}
int val = t1 == null ? t2.val : t2 == null ? t1.val : t1.val + t2.val;
// 没必要新new一个node
TreeNode node = new TreeNode(val);
TreeNode l1 = t1 == null ? null : t1.left;
TreeNode r1 = t1 == null ? null : t1.right;
TreeNode l2 = t2 == null ? null : t2.left;
TreeNode r2 = t2 == null ? null : t2.right;
node.left = mergeTrees(l1, l2);
node.right = mergeTrees(r1, r2);
return node;
}
/**
* 官方题解
* @param t1
* @param t2
* @return
*/
public TreeNode mergeTrees1(TreeNode t1, TreeNode t2) {
// t1为空返回t2,t2为空返回t1
if (t1 == null)
return t2;
if (t2 == null)
return t1;
// t1 t2 都不为空
t1.val += t2.val;
t1.left = mergeTrees1(t1.left, t2.left);
t1.right = mergeTrees1(t1.right, t2.right);
return t1;
}
}