Skip to content

Commit d9d7017

Browse files
refactor 617
1 parent 34b5d07 commit d9d7017

File tree

2 files changed

+65
-30
lines changed

2 files changed

+65
-30
lines changed

src/main/java/com/fishercoder/solutions/_617.java

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,41 @@
3131
*/
3232
public class _617 {
3333

34-
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
35-
if (t1 == null) {
36-
return t2;
34+
public static class Solution1 {
35+
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
36+
if (t1 == null) {
37+
return t2;
38+
}
39+
if (t2 == null) {
40+
return t1;
41+
}
42+
TreeNode mergedNode = null;
43+
if (t1 != null && t2 != null) {
44+
mergedNode = new TreeNode(t1.val + t2.val);
45+
} else if (t1 != null) {
46+
mergedNode = t1;
47+
} else if (t2 != null) {
48+
mergedNode = t2;
49+
}
50+
mergedNode.left = mergeTrees(t1.left, t2.left);
51+
mergedNode.right = mergeTrees(t1.right, t2.right);
52+
return mergedNode;
3753
}
38-
if (t2 == null) {
39-
return t1;
40-
}
41-
TreeNode mergedNode = null;
42-
if (t1 != null && t2 != null) {
43-
mergedNode = new TreeNode(t1.val + t2.val);
44-
} else if (t1 != null) {
45-
mergedNode = t1;
46-
} else if (t2 != null) {
47-
mergedNode = t2;
54+
}
55+
56+
public static class Solution2 {
57+
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
58+
if (t1 == null) {
59+
return t2;
60+
}
61+
if (t2 == null) {
62+
return t1;
63+
}
64+
TreeNode mergedNode = new TreeNode(t1.val + t2.val);
65+
mergedNode.left = mergeTrees(t1.left, t2.left);
66+
mergedNode.right = mergeTrees(t1.right, t2.right);
67+
return mergedNode;
4868
}
49-
mergedNode.left = mergeTrees(t1.left, t2.left);
50-
mergedNode.right = mergeTrees(t1.right, t2.right);
51-
return mergedNode;
5269
}
5370

5471
}
Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,55 @@
11
package com.fishercoder;
22

33
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
45
import com.fishercoder.solutions._617;
56
import org.junit.BeforeClass;
67
import org.junit.Test;
78

9+
import java.util.Arrays;
10+
11+
import static junit.framework.TestCase.assertEquals;
12+
813
/**
914
* Created by stevesun on 6/10/17.
1015
*/
1116
public class _617Test {
12-
private static _617 test;
17+
private static _617.Solution1 solution1;
18+
private static _617.Solution2 solution2;
1319
private static TreeNode t1;
1420
private static TreeNode t2;
1521
private static TreeNode actual;
22+
private static TreeNode expected;
1623

1724
@BeforeClass
1825
public static void setup() {
19-
test = new _617();
26+
solution1 = new _617.Solution1();
27+
solution2 = new _617.Solution2();
2028
}
2129

2230
@Test
2331
public void test1() {
24-
t1 = new TreeNode(1);
25-
t1.left = new TreeNode(3);
26-
t1.right = new TreeNode(2);
27-
t1.left.left = new TreeNode(5);
28-
29-
t2 = new TreeNode(2);
30-
t2.left = new TreeNode(1);
31-
t2.right = new TreeNode(3);
32-
t2.left.right = new TreeNode(4);
33-
t2.right.right = new TreeNode(7);
34-
35-
actual = test.mergeTrees(t1, t2);
32+
t1 = TreeUtils.constructBinaryTree(Arrays.asList(1, 3, 2, 5));
33+
34+
t2 = TreeUtils.constructBinaryTree(Arrays.asList(2, 1, 3, null, 4, null, 7));
35+
36+
expected = TreeUtils.constructBinaryTree(Arrays.asList(3, 4, 5, 5, 4, null, 7));
37+
38+
actual = solution1.mergeTrees(t1, t2);
39+
40+
assertEquals(expected, actual);
41+
}
42+
43+
@Test
44+
public void test2() {
45+
t1 = TreeUtils.constructBinaryTree(Arrays.asList(1, 3, 2, 5));
46+
47+
t2 = TreeUtils.constructBinaryTree(Arrays.asList(2, 1, 3, null, 4, null, 7));
48+
49+
expected = TreeUtils.constructBinaryTree(Arrays.asList(3, 4, 5, 5, 4, null, 7));
50+
51+
actual = solution2.mergeTrees(t1, t2);
52+
53+
assertEquals(expected, actual);
3654
}
3755
}

0 commit comments

Comments
 (0)